关于动态生成的文本域取值问题,谢谢。
//js中动态拼接出的下面的html代码
<input type="hidden" value="${student.id}"/>
<input type="hidden" value="${student.name}"/>
<input type="hidden" value="${student.age}"/>
这些隐藏域都是通过取后台值,然后动态生成的,也就是说个数不确定,我现在想问的是怎么取到这些的值啊,谢谢。然后组成一个字符串,也就是说类似下面的
student1.id:student1.name:student1.age;student2.id:student2.name:student2.age;...后面还有好多好多
这个 冒号和分号就是为了区别,可以任意搞,我只是为了方便。
1.取值
2.组成字符串
请问咋弄 谢谢。
可以用jquery或者原生的都行。
//js中动态拼接出的下面的html代码
<input type="hidden" value="${student.id}"/>
<input type="hidden" value="${student.name}"/>
<input type="hidden" value="${student.age}"/>
这些隐藏域都是通过取后台值,然后动态生成的,也就是说个数不确定,我现在想问的是怎么取到这些的值啊,谢谢。然后组成一个字符串,也就是说类似下面的
student1.id:student1.name:student1.age;student2.id:student2.name:student2.age;...后面还有好多好多
这个 冒号和分号就是为了区别,可以任意搞,我只是为了方便。
1.取值
2.组成字符串
请问咋弄 谢谢。
可以用jquery或者原生的都行。
<div id="contaniner" style="display:none">
<div style="display:none">
<input type="hidden" value="${student.id}"/>
<input type="hidden" value="${student.name}"/>
<input type="hidden" value="${student.age}"/>
</div>...
....
...
<div style="display:none">
<input type="hidden" value="${student.id}"/>
<input type="hidden" value="${student.name}"/>
<input type="hidden" value="${student.age}"/>
</div>
</div>然后进行循环
var s=""
var ds = document.getElementById("contaniner");
for(i=0;i<ds.length;i++){
s +="student" + i + ":" + ds[i].getElementByTagName("INPUT")[0].value + "," + ds[i].getElementByTagName("INPUT")[1].value
}类似这样当然,还有很多其他的方法
谢谢
您说的是在外层加一个div
<div id="contaniner" style="display:none">
这个会生成很多很多的div id="contaniner" style="display:none">吧然后
var ds = document.getElementById("contaniner");这个 能获取到嘛,这么多div的id都是contaniner谢谢。
<div id="contaniner" style="display:none">
<div style="display:none"><input...></div>
<div style="display:none"><input...></div>
<div style="display:none"><input...></div>
....
</div>var ds = document.getElementById("contaniner");我写错了。应该是
var ds = document.getElementById("contaniner").getElementsByTagName("div");
var vals = [];
$("input[type='hidden']").each(function(i,el){
vals.push($(el).val());
});
var str = vals.join(":");