动态创建的元素可不可以通过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.
-----------------
以下是我的代码:<!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.
<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>
function get(){
/*var ccc=document.getElementsByName("bbb");
//var ccc=document.getElementById("bbb1");
alert(ccc[0].value);*/
alert(document.getElementById("aaa").innerHTML)
}
原因.
document.createElement("input"); 这种方法是不是不能通过NAME属性获取对象。
<!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>
说的是.要有name属性的话.不能用设置,要直接写在creatElement的参数中.caiying2009的方法也行.因为name变为自定义属性存在DOM对象了.不过还要封装一下.
不行。。
oInput.id="bbb1";
oInput.name="bbb";
和
oInput.setAttribute("id","bbb1");
oInput.setAttribute("name","bbb");
效果是一样的
都报0.value为空或不是对象
他那样比较复杂,
要循环。
因为我要创建10个左右input,text
我 还是用你那种方法吧。第一种也行,非常感谢!!