document.createElement("INPUT")初级问题!

zunyir 2007-10-16 04:12:42
<script>
function createInput(){
if(document.all.jsq.value=='')
{
document.all.jsq.value=1;
}
else
document.all.jsq.value=document.all.jsq.value+1;
var sn='arrr'+document.all.jsq.value;

var a=document.createElement("<INPUT onChange=createInput()>");
a.setAttribute("type","text");
a.setAttribute("name","sn");
a.setAttribute("id","sn");
document.body.appendChild(a);

var sww='arrr'+document.all.jsq.value;
alert(document.getElementById("sn").value)//正确的

alert(document.getElementById("sww").value);//错误的!
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body>

<script>createInput()</script>
<form name="form1" method="post" action="">
<input type="text" name="var1" onChange="createInput()">
<input type="text" name="jsq" value=1>
<input type="text" name="temp1">
<input type="submit" name="Submit" value="提交">
</form>
<div id="content"></div>
</body>
</html>


____________________________________

没有明白怎么都是一样的,一个对,一个说没有对象,我是初学者!想得到所有动态input中的值!
...全文
1250 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoyang22 2007-10-16
  • 打赏
  • 举报
回复
<script >
function createInput(){
if(document.all.jsq.value== '')
{
document.all.jsq.value=1;
}
else
document.all.jsq.value=document.all.jsq.value+1;
var sn= 'arrr'+document.all.jsq.value;
var a=document.createElement("<INPUT onChange=createInput() >");
a.setAttribute("type","text");
a.setAttribute("name","sn");
a.setAttribute("id",sn);
//a.setAttribute("value",document.all.jsq.value);
document.body.appendChild(a);

var sww= 'arrr'+document.all.jsq.value;
alert(document.getElementById(sn).value)//正确的

alert(document.getElementById(sww).value);//错误的!
}
</script >
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" > </head >
<body >

<script >createInput() </script >
<form name="form1" method="post" action="" >
<input type="text" id="var1" onChange="createInput()" >
<input type="text" id="jsq" value=1 >
<input type="text" id="temp1" >
<input type="submit" name="Submit" value="提交" >
</form >
<div id="content" > </div >
</body >
</html >
gray820928 2007-10-16
  • 打赏
  • 举报
回复
alert(document.getElementById("sn").value)//正确的
这是因为你生成的那个text就是叫sn

alert(document.getElementById("sww").value);//错误的!
这是因为没有叫sww的对象
gray820928 2007-10-16
  • 打赏
  • 举报
回复
alert(document.getElementById("sn").value)//正确的
这是因为你生成的那个text就是叫sn

alert(document.getElementById("sww").value);//错误的!
这是因为没有叫sww的对象
jupiterhelen 2007-10-16
  • 打赏
  • 举报
回复
a.setAttribute("id","sn");
你每次Create新的INPUT 的Id都是这个。自然document.getElementById("sww")是空
publina 2007-10-16
  • 打赏
  • 举报
回复
你要实现什么功能?
alert(document.getElementById("sww").value);

getElementById 根据ID取对象, sww是誰的ID

var sww= 'arrr '+document.all.jsq.value 这样得出来是誰的ID?

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧