JS代码
$(function () { //获取所有选中部门 $("#Chkblt_ImpactDetpName input[type='checkbox']").bind("click", function () { var ids = ""; var names = ""; $("#Chkblt_ImpactDetpName input[type='checkbox']:checked").each(function () { ids += $(this).attr("value") + ","; names += $(this).next("label").text() + ","; }); $("#hid_ImpactDetpNo").val(ids); $("#hid_ImpactDetpName").val(names); }); //赵孟蒙 2018-08-29 绑定复选框值 var hid_ImpactDetpNo = $("#hid_ImpactDetpNo").val().split(','); //var hid_ImpactDetpName = $("#hid_ImpactDetpName").val().split(','); var arr = hid_ImpactDetpNo; if (arr.length > 0) { //如有则进行循环赋值 for (var i = 0; i < arr.length - 1; i++) { $("#Chkblt_ImpactDetpName input[type='checkbox']").each(function () { if ($(this).val() == arr[i]) { $(this).attr("checked", true); } }); } } });
前台代码:
<tr> <td> <span class="red">*</span>受影响部门 </td> <td colspan="3"> <asp:CheckBoxList ID="Chkblt_ImpactDetpName" lang="Chk" runat="server" RepeatColumns="6" Width="97%"> </asp:CheckBoxList> <input type="hidden" id="hid_ImpactDetpNo" runat="server" /> <input type="hidden" id="hid_ImpactDetpName" runat="server" /> </td> </tr>
后台代码加载复选框:
Chkblt_ImpactDetpName.DataSource = ds.Tables[3]; Chkblt_ImpactDetpName.DataTextField = "DepartName"; Chkblt_ImpactDetpName.DataValueField = "DepartId"; Chkblt_ImpactDetpName.DataBind();
后台代码读取初始值:
//受影响部门 txt_RiskAssessment.Value= ds.Tables[0].Rows[0]["RiskAssessment"].ToString(); hid_ImpactDetpNo.Value = ds.Tables[0].Rows[0]["ImpactDetpNo"].ToString();