获取动态创建元素的值的方法

luck_man911 2009-05-13 02:49:48
动态创建的元素可不可以通过name属性获取,如:document.getElementsByName("bbb")[0].value???
-----------------
以下是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function add(){
var oInput=document.createElement("input");
oInput.setAttribute("type","text");
oInput.setAttribute("value","1234567");
oInput.setAttribute("id","bbb1");
oInput.setAttribute("name","bbb");
var aaa=document.getElementById("aaa");
aaa.appendChild(oInput);

}
function get(){
var ccc=document.getElementsByName("bbb");
//var ccc=document.getElementById("bbb1");
alert(ccc[0].value);

}
</script>
</head>

<body>
<input type="button" onclick="get();" value="name取得值" /><br />
<input type="text" value="dddddddd333" name="ttt"/><br/>
<input type="button" onclick="add();" value="增加" /><br />
<div id="aaa">
</div>

</body>
</html>


分析:如果通过动态元素的ID,是可以取得动态元素的value.
...全文
155 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luck_man911 2009-05-13
  • 打赏
  • 举报
回复
哦。。
他那样比较复杂,
要循环。
因为我要创建10个左右input,text
我 还是用你那种方法吧。

第一种也行,

非常感谢!!
natineprince 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 luoqinglong850102 的回复:]
caiying2009的方法
不行。。
oInput.id="bbb1";
oInput.name="bbb";

oInput.setAttribute("id","bbb1");
oInput.setAttribute("name","bbb");
效果是一样的。。。
都报0.value为空或不是对象。。。
[/Quote]
你看他取值...不是用getElementsByName的...
luck_man911 2009-05-13
  • 打赏
  • 举报
回复
caiying2009的方法
不行。。
oInput.id="bbb1";
oInput.name="bbb";

oInput.setAttribute("id","bbb1");
oInput.setAttribute("name","bbb");
效果是一样的。。。
都报0.value为空或不是对象。。。
natineprince 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luoqinglong850102 的回复:]
谢谢指点。

但是,我想用这个创建元素
document.createElement("input");

这种方法是不是不能通过NAME属性获取对象。。。。
[/Quote]
我上面那段是出自HTML参考手册.
说的是.要有name属性的话.不能用设置,要直接写在creatElement的参数中.

caiying2009的方法也行.因为name变为自定义属性存在DOM对象了.不过还要封装一下.
caiying2009 2009-05-13
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function add(){
var oInput=document.createElement("input");
oInput.setAttribute("type","text");
oInput.setAttribute("value","1234567");
oInput.id="bbb1";
oInput.name="bbb";
var aaa=document.getElementById("aaa");
aaa.appendChild(oInput);

}
function get(){
var obj=document.getElementsByTagName("input")
for (var i=0;i<obj.length;i++)
if (obj[i].name=="bbb")
alert(obj[i].value)


}
</script>
</head>

<body>
<input type="button" onclick="get();" value="name取得值" /><br />
<input type="text" value="dddddddd333" name="ttt"/><br/>
<input type="button" onclick="add();" value="增加" /><br />
<div id="aaa">
</div>

</body>
</html>
luck_man911 2009-05-13
  • 打赏
  • 举报
回复
谢谢指点。

但是,我想用这个创建元素
document.createElement("input");

这种方法是不是不能通过NAME属性获取对象。。。。
natineprince 2009-05-13
  • 打赏
  • 举报
回复
Attributes can be included with the sTag as long as the entire string is valid HTML. You should do this if you wish to include the NAME attribute at run time on objects created with the createElement method.

so you should do as below as you wanna Name attribute

newInput = document.createElement("<INPUT TYPE='text' NAME='test' VALUE='123'>")
natineprince 2009-05-13
  • 打赏
  • 举报
回复

function get(){
/*var ccc=document.getElementsByName("bbb");
//var ccc=document.getElementById("bbb1");
alert(ccc[0].value);*/
alert(document.getElementById("aaa").innerHTML)
}

原因.
s_liangchao1s 2009-05-13
  • 打赏
  • 举报
回复

<script type="text/javascript">
function add(){
document.getElementById("aaa").innerHTML = "<input type='text' name='bbb' value='123456' />";
}
function get(){
alert(document.getElementsByName("bbb")[0].value);

}
</script>

87,910

社区成员

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

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