1、
View Code
function checkSelectedAll(thischeck) { for (var i = 0; i < form1.elements.length; i++) { var e = form1.elements[i]; if (e.name == 'id') { e.checked=thischeck; } } return false; } <input type="checkbox" id="checkall" onclick="checkSelectedAll(this.checked)" />全选/取消
2、
View Code
<script type="text/javascript"> var carcount=<%=carCount %>; var arrcategory=<%=arrCategory %>; var selectedcar=<%=selectedCar %>; for (var j=0;j<selectedcar.length;j++) { for (var i=0;i<carcount;i++){ $("#selectCar").append( '<input type="checkbox" id="Car'+i+'" name="id" value="'+arrcategory[i][1]+'" /> '+arrcategory[i][0]+'<br \>' ) if ($("#Car"+i).val()==selectedcar[j]){ $("#Car"+i).attr("checked","checked"); } } } </script>
3、CheckBoxList全选(火狐浏览器无效)
View Code
<input type="checkbox" id="checkall" onclick="selectAll('cblCarModels')" />全选/取消 <asp:CheckBoxList ID="cblCarModels" runat="server" /> function selectAll(searchName){ var aa = document.getElementsByTagName("input"); for (var i=0; i<aa.length; i++) { if (aa[i].type=='checkbox'&&aa[i].name.indexOf(searchName)>-1) { aa[i].checked=event.srcElement.checked; } } }
后台绑定数据:
View Code
protected void Page_Load(object sender, EventArgs e) { Member member = (Member)SessionManager.Member; if (member == null) { Response.Redirect("Login.aspx"); return; } if (!IsPostBack) { cblCarModels.DataSource = DictBusiness.FindAllByCategory(EnmDictCategory.车系); cblCarModels.DataTextField = "Value"; cblCarModels.DataValueField = "Code"; cblCarModels.RepeatColumns = 2; cblCarModels.RepeatDirection = RepeatDirection.Horizontal; cblCarModels.DataBind(); foreach (ListItem carModel in cblCarModels.Items) { if (checkboxSelect(carModel.Value)) { carModel.Selected = true; } } } }
4\直接全选。
View Code
function checkSelected(ischecked) { var allcheckbox = $(":checkbox"); allcheckbox.attr("checked", ischecked); } <input type="checkbox" id="checkall" onclick="checkSelected(this.checked)" />全选/取消