读书人

一象引用解决方案

发布时间: 2012-03-20 14:01:10 作者: rapoo

一象引用
下面的代:一次按,增加一行,面有input,名字aa
什麽法用document.frm.aa引用到input象?怎才可以呢。


<script>
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);


}
function createCellWithInput(name){
var cell=document.createElement( "TD ");
var input=document.createElement( "input ");
input.setAttribute( "type ", "text ");
input.setAttribute( "name ",name);

cell.appendChild(input);
return cell;
}


</script>
<form name=frm>
<table> <tbody id= "TAB "> </tbody> </table>

<input type=button onclick= "appendNewRow();alert(document.frm.aa) ">
</form>

[解决办法]
<input type=button onclick= "appendNewRow();alert(document.frm.aa) ">
改为:
<input type=button onclick= "appendNewRow();alert(document.all.frm.aa) ">

[解决办法]
input.setAttribute( "name ", "aa ");
改成
input.setAttribute( "id ", "aa ");
就能用了
[解决办法]
<script>
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>
------解决方案--------------------


<html>
<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属性直接引用,浏览器不识别~~

读书人网 >JavaScript

热点推荐