跪求用js写限制table里的checkbox每行的复选框的选择,并且每行限制的个数不同
如图所示: 这个用table写的 checkbox放在<td>标签里,我之前写的那个是table里全部的复选框,多选了会提示只能选几个。代码是这样的:
<script type="text/javascript">
function setCheckValue() {
var obj = document.getElementById("MainContent_hdTimesCheckValue");
var cheked_num=0; //准备选择的checkbox
var hadcheked_num=0; //已经选择的checkbox
var count_num = <%=lg.ToInt(practiceCountTimes)%>;
if (obj != undefined && obj != null) {
var chk = document.getElementsByName("chkPracticeTimes");
if (chk != undefined && chk != null) {
obj.value = "";
if (chk.length == undefined) {
if (chk.checked) obj.value = chk.valueOf;
} else {
for (var i = 0; i < chk.length; i++) {
if (chk[i].checked) {
cheked_num+=1;
if (count_num!=0&&cheked_num>count_num) {
alert("最多只能选"+count_num+"个!");
event.srcElement.checked = false;
// cheked_num--;
i--;
}
obj.value += "|" + chk[i].value;
}
}
}
}
}
}
</script>
现在是想要根据日期限制每行复选框的的选项,就是table里每行的复选框都限制不一样的,选多了会提示只能选多少个的那种。
已经折腾几天了,有哪位js高手帮帮忙吧 ,在痛苦中!!我现在只有这么多分了,因为比较急,所以没去换分呢,希望帮帮忙吧,谢谢啦!! JavaScript CheckBox table
[解决办法]
<html class="">
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script>
$(function(){
$("table.table1 input[type='checkbox']").click(function(){
var tr=$(this).closest("tr");
var maxcheckedLen=~~(tr.attr("maxChecked"));
var checkedLen=$("input[type='checkbox']:checked",tr).length;
if(maxcheckedLen<checkedLen){
alert("超过最大限制!");
$(this).prop("checked",false);
}
});
});
function getValue(){
var values=[];
$("input[type='checkbox']:checked").each(function(){
values.push($(this).val());
});
alert(values.join('
[解决办法]
'));
}
</script>
</head>
<body>
<input width="80px" type="button" onclick="getValue();" value='GetValue' />
<table width="977" border="1" class="table1">
<tr maxChecked="2">
<td><input type="checkbox" value=31 ></td>
<td><input type="checkbox" value=21></td>
<td><input type="checkbox" value=15></td>
<td><input type="checkbox" value=18></td>
<td><input type="checkbox" value=17></td>
</tr>
<tr maxChecked="3">
<td><input type="checkbox" value=14></td>
<td><input type="checkbox" value=13></td>
<td><input type="checkbox" value=12></td>
<td><input type="checkbox" value=11></td>
<td><input type="checkbox" value=10></td>
</tr>
</table>
</body>
</html>
加了个取值的方法