Home  >  Article  >  Web Front-end  >  动态生成的html的Table中第一列是checkbox,如何判断选择了哪些checkbox,并获得相对应的记录_html/css_WEB-ITnose

动态生成的html的Table中第一列是checkbox,如何判断选择了哪些checkbox,并获得相对应的记录_html/css_WEB-ITnose

PHP中文网
PHP中文网Original
2016-06-24 12:08:171358browse

动态生成的html的Table中第一列是checkbox,如何判断选择了哪些checkbox,并获得相对应的记录??  


js代码:  

function get(){
var print = document.getElementsByName("isprint");
for(var i = 0; i < print.length; i++) {
if(print[i].checked) {
var factorId = document.getElementById(&#39;factorId&#39;).value;
alert(factorId);
} 
}
}

<table class="grid">
<tfoot></tfoot> 
<tbody>
<tr>
<th width="30px" >序号</th>
<th>标签要素名称</th>            
<th>是否起用</th>     
</tr>
<s:iterator value="%{pageDataFactorDef.datals}" id=&#39;vo&#39; status=&#39;st&#39;>
<td align="center"><s:property value="#st.count+(pageDataFactorDef.currentPage-1) * 
(pageDataFactorDef.pageSize)" /></td>
<td>${vo.factorName}</td>
<td>
<input type="checkbox" name="isprint" id="isprint" />
</td>
<td align="center"> <input id="factorId" value="${vo.factorId}" type="hidden"></input>//  取得隐藏域id值
   
 </tr> 
</s:iterator>       
</tbody>
</table>

在js中取得的id永远是table表中的第一条记录,不管你选中几条数据,它都是显示第一条记录数据的id值。


回复讨论(解决方案)

你所有的隐藏域的id都一样 当然取第一个

那怎么取得被选中相对应的数据

<input type="checkbox" name="isprint" id="isprint" cmdValue="${vo.factorId}" />
if(print[i].checked) {
alert(print[i].getAttribute("cmdValue"));
}
<input type="checkbox" name="isprint" id="isprint" />

改成

<input type="checkbox" name="isprint<%# Container.ItemIndex+1 %>" id="isprint<%# Container.ItemIndex+1 %>" />

92cee25da80fac49f6fb6eec5fd2c22a中加个ID

JS搜92cee25da80fac49f6fb6eec5fd2c22a 的行数

var rowCount = document.all.tbodyDetail.children.length - 1;
for (var i = 1; i <= rowCount; i++) {
var checkbox= form1["checkbox" + i].value.trim();
}

然后你遍历这个rowCount
你就可以拿到checkbox的id了 

以上就是动态生成的html的Table中第一列是checkbox,如何判断选择了哪些checkbox,并获得相对应的记录_html/css_WEB-ITnose的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn