Javascript 通过DOM添加表单元素
如果将表单元素添加到现有表单中,所有示例都使用document.getElementById方法来抓取要将表单元素附加到的元素,或者它们是Jquery方法,我不使用,也不需要使用Jquery 这是我的表格Javascript 通过DOM添加表单元素,javascript,forms,dom,Javascript,Forms,Dom,如果将表单元素添加到现有表单中,所有示例都使用document.getElementById方法来抓取要将表单元素附加到的元素,或者它们是Jquery方法,我不使用,也不需要使用Jquery 这是我的表格 <form name="formname" action="javascript:return false;" enctype="multipart/form-data" method="post" onSubmit="processCart(this);"> <ta
<form name="formname" action="javascript:return false;" enctype="multipart/form-data" method="post" onSubmit="processCart(this);">
<table>
<tr>
<td> </td>
<td>Number </td>
<td>Price </td>
<td>Quantity </td>
<td>Remove</td>
</tr>
<tr>
<td> <input type="checkbox" name="items" value="Item 1"></td>
<td># 1 </td>
<td>$<input type="text" name="price" size="12" value="1.00" readonly /></td>
<td><input type="text" name="quantities" value=""/></td>
<td> </td>
}
函数加法器
function addElement(){
try{
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("name", arguments[0]);
element.setAttribute("type", arguments[1]);
element.setAttribute("value", arguments[2]);
document.formname.appendChild(element);
}catch(e){
alert( e );
}
}
我没有警报,也没有看到任何元素被添加,所以有人可以帮助解决可能出现的问题吗?for(I in ele){…what's ele..?您似乎正在使用一些参数调用
addElement
,但在您的函数定义中它没有(这是许多似乎错误的事情之一)。
function addElement(){
try{
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("name", arguments[0]);
element.setAttribute("type", arguments[1]);
element.setAttribute("value", arguments[2]);
document.formname.appendChild(element);
}catch(e){
alert( e );
}