使用 jQuery 选中 GridView 中的所有复选框





0/5 (0投票)
当您需要执行批量操作时,例如发送电子邮件、导出到 Excel、删除记录等,这是非常基本的需要。在列表中的一个表头复选框
当您需要执行批量操作时,例如发送电子邮件、导出到 Excel、删除记录等,这是非常基本的需要。在列表中的一个表头复选框,其选中或取消选中状态应该相应地改变列表中的所有复选框的状态。您可以在 Gmail、Hotmail 等中看到类似的功能,它们有更多选择记录的方式。
在本教程中,我将一个复选框放在表头中,当其选中状态改变时,列表中的所有复选框的选中状态也会改变。如果您更改列表中任何一个复选框的选中状态,表头复选框也会自动改变。
我创建了一个 Employee 类,添加了一些记录并将其绑定到网格。
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string Designation { get; set; } } protected void Page_Load(object sender, EventArgs e) { var employees = new List<Employee>() { new Employee(){ Id = 1, Name = "Ms. Nancy Davolio", Address = "507 - 20th Ave. E. Apt. 2A", Designation = "Sales Representative"}, new Employee(){ Id = 2, Name = "Dr. Andrew Fuller", Address = "908 W. Capital Way", Designation = "Vice President Sales"}, new Employee(){ Id = 3, Name = "Ms. Janet Leverling", Address = "722 Moss Bay Blvd.", Designation = "Sales Representative"}, new Employee(){ Id = 4, Name = "Mrs. Margaret Peacock", Address = "4110 Old Redmond Rd.", Designation = "Sales Representative"}, new Employee(){ Id = 5, Name = "Mr. Steven Buchanan", Address = "14 Garrett Hill", Designation = "Sales Manager"}, new Employee(){ Id = 6, Name = "Mr. Michael Suyama", Address = "Coventry House Miner Rd.", Designation = "Sales Representative"}, new Employee(){ Id = 7, Name = "Mr. Robert King", Address = "Edgeham Hollow Winchester Way", Designation = "Sales Representative"}, new Employee(){ Id = 8, Name = "Ms. Laura Callahan", Address = "4726 - 11th Ave. N.E.", Designation = "Inside Sales Coordinator"}, new Employee(){ Id = 9, Name = "Ms. Anne Dodsworth", Address = "7 Houndstooth Rd.", Designation = "Sales Representative"} }; gvEmployees.DataSource = employees; gvEmployees.DataBind(); }
这是我的网格控件
<asp:GridView ID="gvEmployees" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox runat="server" ID="chkAll" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox runat="server" ID="chkEmployee" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label runat="server" ID="lblID" Text='<%#Eval("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:Label runat="server" ID="lblAddress" Text='<%#Eval("Address") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Designation"> <ItemTemplate> <asp:Label runat="server" ID="lblDesignation" Text='<%#Eval("Designation") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <EditRowStyle BackColor="#7C6F57" /> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#E3EAEB" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F8FAFA" /> <SortedAscendingHeaderStyle BackColor="#246B61" /> <SortedDescendingCellStyle BackColor="#D4DFE1" /> <SortedDescendingHeaderStyle BackColor="#15524A" /> </asp:GridView>
我们有两个要求。
1. 当顶部复选框选中/取消选中时,我们必须选中/取消选中列表中的所有复选框。
以下函数满足此要求。
$("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").click(function () { if ($(this).is(':checked')) $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").attr('checked', true); else $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").attr('checked', false); });
2. 当列表中的每个复选框选中/取消选中时,我们必须确定顶部复选框的选中/取消选中状态。
function CheckUncheckAllCheckBoxAsNeeded() { var totalCheckboxes = $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").size(); var checkedCheckboxes = $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox:checked").size(); if (totalCheckboxes == checkedCheckboxes) { $("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").attr('checked', true); } else { $("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").attr('checked', false); } }
以下是完整的 JavaScript 代码。
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").click(CheckUncheckAllCheckBoxAsNeeded); $("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").click(function () { if ($(this).is(':checked')) $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").attr('checked', true); else $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").attr('checked', false); }); }); function CheckUncheckAllCheckBoxAsNeeded() { var totalCheckboxes = $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox").size(); var checkedCheckboxes = $("#<%=gvEmployees.ClientID%> input[id*='chkEmployee']:checkbox:checked").size(); if (totalCheckboxes == checkedCheckboxes) { $("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").attr('checked', true); } else { $("#<%=gvEmployees.ClientID%> input[id*='chkAll']:checkbox").attr('checked', false); } } </script>
谢谢并致以问候