日期:2014-05-18  浏览次数:20385 次

CheckBox全选与反选,用JS实现
要求:点击cb1对cblist1进行选择转换;如果点击cb2就对cblist2进行选择转换。

已有客户端代码:
<asp:CheckBox runat="server" ID="cb1" Checked="true" Text="全选"/>
<S:CheckBoxListControl runat="server" ID="cblist1" RepeatColumns="5"/>

<asp:CheckBox runat="server" ID="cb2" Checked="true" Text="全选"/>
<S:CheckBoxListControl runat="server" ID="cblist2" RepeatColumns="5"/>

已有服务端代码:
cb1.Attributes["onClick"] = "cbOnclick()";
cb2.Attributes["onClick"] = "cbOnclick()";

给这两个加的同一个点击事件,()之间肯定要传值对点击的控件进行区分的。问题就是不知道传什么。还有funtion里面怎么写?

------解决方案--------------------
cb1.Attributes["onClick"] = "cbOnclick(this,'" + cblist1.ClientID + "')";
cb2.Attributes["onClick"] = "cbOnclick(this,'" + cblist2.ClientID + "')";


function cbOnclick(x, y) {
xs = document.getElementById(y).getElementsByTagName("input")
for (i = 0; i < xs.length; i++) {
if (xs[i].type == "checkbox") xs[i].checked = x.checked
}
}
------解决方案--------------------
cb1.Attributes["onClick"] = "cbOnclick(this,'" + cblist1.ClientID + "')";
cb2.Attributes["onClick"] = "cbOnclick(this,'" + cblist2.ClientID + "')";


function cbOnclick(x, y) {
xs = document.getElementById(y).getElementsByTagName("input")
for (i = 0; i < xs.length; i++) {
if (xs[i].type == "checkbox") xs[i].checked = x.checked
}
}
------解决方案--------------------
全选 function checkboxall(){
$("input[@type='checkbox']").each(function(){
$(this).attr("checked",true);
});
  
}
------解决方案--------------------
反选

function rcheckboxall(){
$("input[@type='checkbox']").each(function(){
if($(this).attr("checked")){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
  
}
------解决方案--------------------
<script language="JavaScript" type="text/javascript">
function checkall(form){
for (var i=0;i<form.elements.length;i++){
var e = form.elements[i];
if (e.name != 'chkall') e.checked = form.chkall.checked; 
}
}
</script>

在你需要的地方onClick="checkall(this.form)"调用即可
------解决方案--------------------
探讨

引用:
cb1.Attributes["onClick"] = "cbOnclick(this,'" + cblist1.ClientID + "')";
cb2.Attributes["onClick"] = "cbOnclick(this,'" + cblist2.ClientID + "')";


function cbOnclick(x, y) {
xs ……