怎样用input type="checkbox"来控制CheckBoxList的全选和反选
1个回答
展开全部
这个要用到jquery了 我把php和html编码发给你 jquery.js这个文件你自己下载啊、
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"><!--
function getCheckedNum(){
var count = 0;
$(":checkbox[name=pro\\[\\]]").each(function(){
if($(this).attr("checked")){
count++;
}
});
return count;
}
function changeButtonStatus(){
/*
if(getCheckedNum()){
$(":button").attr("disabled",false);
} else {
$(":button").attr("disabled",true);
}*/
$(":button").attr("disabled",!getCheckedNum());
}
$(function(){
$("#ctlbox").click(function(){
$(":checkbox[name=pro\\[\\]]").attr("checked",$(this).attr("checked"));
changeButtonStatus();
});
$(":checkbox[name=pro\\[\\]]").click(function(){
/*
if($(":checkbox[name=pro\\[\\]]").length == getCheckedNum()){
$("#ctlbox").attr("checked",true);
} else {
$("#ctlbox").attr("checked",false);
}*/
$("#ctlbox").attr("checked",$(":checkbox[name=pro\\[\\]]").length == getCheckedNum());
changeButtonStatus();
});
})
</script>
</head>
<body>
<form action="a.php" method="post" name="form1">
<table width="100%" cellpadding="10" cellspacing="0" border="1">
<tr>
<td width="30"><input type="checkbox" name="ctlbox" id="ctlbox"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="4">
<button type="submit" name="submit" id="submit" disabled="disabled">确认</button>
<button type="reset" name="reset" id="reset" disabled="disabled">取消</button>
<button type="button" name="button" id="button" disabled="disabled">自定义</button>
</td>
</tr>
</table>
</form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"><!--
function getCheckedNum(){
var count = 0;
$(":checkbox[name=pro\\[\\]]").each(function(){
if($(this).attr("checked")){
count++;
}
});
return count;
}
function changeButtonStatus(){
/*
if(getCheckedNum()){
$(":button").attr("disabled",false);
} else {
$(":button").attr("disabled",true);
}*/
$(":button").attr("disabled",!getCheckedNum());
}
$(function(){
$("#ctlbox").click(function(){
$(":checkbox[name=pro\\[\\]]").attr("checked",$(this).attr("checked"));
changeButtonStatus();
});
$(":checkbox[name=pro\\[\\]]").click(function(){
/*
if($(":checkbox[name=pro\\[\\]]").length == getCheckedNum()){
$("#ctlbox").attr("checked",true);
} else {
$("#ctlbox").attr("checked",false);
}*/
$("#ctlbox").attr("checked",$(":checkbox[name=pro\\[\\]]").length == getCheckedNum());
changeButtonStatus();
});
})
</script>
</head>
<body>
<form action="a.php" method="post" name="form1">
<table width="100%" cellpadding="10" cellspacing="0" border="1">
<tr>
<td width="30"><input type="checkbox" name="ctlbox" id="ctlbox"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="checkbox" name="pro[]"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="4">
<button type="submit" name="submit" id="submit" disabled="disabled">确认</button>
<button type="reset" name="reset" id="reset" disabled="disabled">取消</button>
<button type="button" name="button" id="button" disabled="disabled">自定义</button>
</td>
</tr>
</table>
</form>
</body>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询