<input type=button onclick="appendNewRow();alert(document.frm.aa)">
改为:
<input type=button onclick="appendNewRow();alert(document.all.frm.aa)">
改为:
<input type=button onclick="appendNewRow();alert(document.all.frm.aa)">
改成
input.setAttribute("id","aa");
就能用了
to he_8134(求职-广州-C#-1.5k以上) 我現在只要求用name來引用
function appendNewRow(){var row=document.createElement("TR");
var cell=document.createElement("TD");
var input=document.createElement("input");
input.setAttribute("type","text");
input.setAttribute("name","aa"); /////這裡設置名字為aa
cell.appendChild(input);
row.appendChild(cell);
document.getElementById("TAB").appendChild(row);
return input
}
function Alert(ctrl){
alert(ctrl.name+":"+ctrl.value);
}
function AllTextInputInFrm(){
var frm=document.frm;
var inputs=frm.getElementsByTagName("INPUT");
for(var i =0;i<inputs.length;i++){
if(inputs[i].type=='text' && inputs[i].name=="aa"){
alert(inputs[i].name+":"+inputs[i].value);
}
}
}
</script>
<form name=frm>
<table><tbody id="TAB"></tbody></table>
<input type='button' onclick="AllTextInputInFrm();" value="AllTextInputInFrm"/>
<input type=button onclick="Alert(appendNewRow());">
</form>
<body>
<script>
function appendNewRow(){
var input=document.createElement("input");
input.setAttribute("type","text");
input.setAttribute("name","aa");
document.frm.appendChild(input);
alert(document.frm.aa);//输出undefined
alert(document.frm.bb);//输出[object]
}
</script>
<form name="frm">
<input type="text" name="bb" />
</form>
<input type="button" onclick="appendNewRow();" />
</body>
</html>实验结果,动态创建的元素无法用name属性直接引用,浏览器不识别~~