限制每个表格行的复选框选择
问题描述:
我试图限制选定的checboxes每个表行3,使用JS。每一行都有一个分离复选框和5倍的水平,这可以选择只有2,如:限制每个表格行的复选框选择
<tr>
<td>
<input type="checkbox" name="indicator[]">
</td>
<td>
<input type="checkbox" name="graduations[]">
</td>
<td>
<input type="checkbox" name="graduations[]">
</td>
<td>
<input type="checkbox" name="graduations[]">
</td>
<td>
<input type="checkbox" name="graduations[]">
</td>
<td>
<input type="checkbox" name="graduations[]">
</td>
</tr>
实际的代码作为看到的http://www.javascriptkit.com/script/script2/checkboxlimit.shtml:
function checkboxlimit(checkgroup, limit){
var checkgroup=checkgroup
var limit=limit
for (var i=0; i<checkgroup.length; i++){
checkgroup[i].onclick=function(){
var checkedcount=0
for (var i=0; i<checkgroup.length;checkedcount+=(checkgroup[i].checked)? 1 : 0
if (checkedcount>limit){
alert("You can only select a maximum of "+limit+" checkboxes")
this.checked=false
}
}
}
}
我只能限制对整个表。有没有简单的方法来做到这一点?
答
如果你有机会到jQuery的你可以使用parent().siblings()
选中的复选框的#来限制2每排
$(function() {
$('input[name=graduations\\[\\]]').click(function(){
if($(this).parent().siblings().children("input[name=graduations\\[\\]]:checkbox:checked").length >= 2)
this.checked = false;
});
});
所以,只有两个应该在毕业的所有复选框被选中? – 2014-09-28 17:49:48
请检查标题中的拼写。 – 2014-09-28 17:50:28