【问题标题】:How to check/uncheck all asp.net checkbox on single checkbox event?如何在单个复选框事件上选中/取消选中所有 asp.net 复选框?
【发布时间】:2016-03-16 21:35:06
【问题描述】:

我有一个复选框名称 Select All,我想在其上选中/取消选中我所有的复选框 div。

由于我的复选框是 runat=server 的 asp.net 控件,所以我不知道如何在客户端处理它。

这是我的代码:

<asp:CheckBox onchange="Checkallcheckbox()" ID="chkAll" runat="server" Text="Parent" /> -- parent of all checkboxes.

---------Section 1 -----------
<div id="section1">
    <li class="carousel-border">
        <asp:CheckBox ID="chkParent1" runat="server" Text="Check All" /> --Parent of below 2 checkboxes
    </li>
    <li>
        <asp:CheckBox ID="chkchild1" runat="server" Text="Child 1" />
    </li>
    <li>
        <asp:CheckBox ID="chkchild2" runat="server" Text="Child 2" />
    </li>                                
</div>

---------Section 2 -----------
<div id="section2">
    <li class="carousel-border">
        <asp:CheckBox ID="chkParent2" runat="server" Text="Check all" /> --Parent of below 2 checkboxes
    </li>
    <li>
        <asp:CheckBox ID="chkchild3" runat="server" Text="Child 1" />
    </li>
    <li>
        <asp:CheckBox ID="chkchild4" runat="server" Text="Child 2" />
    </li>                                
</div>

function Checkallcheckbox() {
    // How to check/uncheck all checkbox on this event
}

【问题讨论】:

  • 为检查所有复选框提供通用类并将其用作$('.checkAll').on('change', function() { $(this).closest('div').find(':checkbox').prop('checked', this.checked);});
  • CheckAll CheckBox Jquery的可能重复
  • @Tushar:但是我想用控件ID而不是类然后
  • @Learning 使用$('[id^=chkParent]).on('change', function`

标签: javascript c# jquery asp.net checkbox


【解决方案1】:

给它们对应的类名,以便选择它们。
然后,您将能够使用单个选择器执行此操作:

<asp:CheckBox ID="chkAll" runat="server" Text="Parent" />

<div id="section1" class="section">
    <li class="carousel-border">
        <asp:CheckBox ID="chkParent1" Class="chkParent" runat="server" Text="Check All" /> --Parent of below 2 checkboxes
    </li>
    <li>
        <asp:CheckBox ID="chkchild1" Class="chkChild" runat="server" Text="Child 1" />
    </li>
    <li>
        <asp:CheckBox ID="chkchild2" Class="chkChild" runat="server" Text="Child 2" />
    </li>                                
</div>

<div id="section2" class="section">
    <li class="carousel-border">
        <asp:CheckBox ID="chkParent2" Class="chkParent" runat="server" Text="Check all" />
    </li>
    <li>
        <asp:CheckBox ID="chkchild3" Class="chkChild" runat="server" Text="Child 1" />
    </li>
    <li>
        <asp:CheckBox ID="chkchild4" Class="chkChild" runat="server" Text="Child 2" />
    </li>                                
</div>

现在,您可以在 JS 中附加 change 处理程序:

$(document).ready(function() {
    var $chkAll = $("#<%= chkAll.ClientID %>");

    $chkAll.change(function() {
        $(".chkParent, .chkChild").prop('checked', this.checked);
    });

    $(".chkParent").change(function() {
        $(this).closest('.section').find('.chkChild').prop('checked', this.checked);
    });
});

您可能还想添加以下处理程序:

  $(".chkChild, .chkParent").change(function() {
      var allCheckedInGroup =  $(this).closest('.section').find('.chkChild:not(:checked)').length === 0;
      $(this).closest('.section').find('.chkParent').prop('checked', allCheckedInGroup);

      var allChecked = $(".chkParent:not(:checked)").length === 0;
      $chkAll.prop('checked', allChecked);
  });

为了使更改反映在“全选”复选框中。试着点击一下就会明白。

这是有效的JSFiddle demo(没有 ASP.NET)。

【讨论】:

  • 你不能像这样使用 $("#chkAll") 因为它不是一个 html 控件。它是一个 asp.net 控件。所以这个事件不会像这样触发
  • @Learning 哦,你是对的。我已经更新了我的答案。
  • 非常感谢您的回答。它有效,但我遇到了一个问题,请您帮帮我
  • 保重。 asp:CheckBox 呈现为带有复选框的跨度,因此 chkAll 将成为跨度元素的一部分。但是你可以使用
  • @AiApaec:由于 1 个问题,我无法触发 chkall 事件。请您帮我解决一下。
【解决方案2】:

在 ASP.NET(Webforms)中,当您使用这样的服务器控件时:

<asp:CheckBox ID="chkAll" runat="server" Text="Parent" />

在客户端是这样渲染的:

<span id="somprefix_chkAll">
  <input type="checkbox" />
</span>

因此,如果您在 javascript 中使用 $("#") 那么您确实在等待该 span 元素的事件 (id="somprefix_chkAll") 但该事件永远不会将被触发,因为 change 事件仅适用于输入元素 (see jQuery doc.)。

但是你可以轻松解决你的问题,你可以在asp.net中使用一个普通的html控件,也可以是一个服务器控件(通过属性:runat="server"),这意味着你可以访问到后面代码中的控件。例如,您可以这样使用:

<input type="checkbox" id="chkAll" runat="server" />

并且在后面的代码中你可以像往常一样访问:

//assigning:
this.chkAll.Checked = true;
//get:
var isChecked = this.chkAll.Checked;

考虑到上述情况,让我更改您的代码:

function Checkallcheckbox(currentClickedCheckbox) {

	var section1Chks= $('#section1').find(':checkbox');
	var section2Chks= $('#section2').find(':checkbox');
	
	if($(currentClickedCheckbox).is(':checked')) {
		section1Chks.prop('checked', true);
		section2Chks.prop('checked', true);
	} else {
		section1Chks.prop('checked', false);
		section2Chks.prop('checked', false);
	}
}

function CheckAllInSection(currentClickedCheckbox) {
	var $currentChk = $(currentClickedCheckbox);
	var sectionChks= $currentChk.closest('div').find(':checkbox');
	var checkAll = $currentChk.is(':checked');
  sectionChks.prop('checked', checkAll);

}
<input type="checkbox" id="chkAll" onchange="Checkallcheckbox(this)"   runat="server" value="Parent" />
<br />
---------Section 1 -----------
<div id="section1">
    <li class="carousel-border">
	<input type="checkbox" id="chkParent1" runat="server" onchange="CheckAllInSection(this)" value="Check All" />
    </li>
    <li>
        <input type="checkbox" id="chkchild1" runat="server" value="Child 1" />
    </li>
    <li>
        <input type="checkbox" id="chkchild2" runat="server" value="Child 2" />
    </li>                                
</div>

---------Section 2 -----------
<div id="section2">
    <li class="carousel-border">
        <input type="checkbox" id="chkParent2" runat="server" onchange="CheckAllInSection(this)" value="Check All" />
    </li>
    <li>
        <input type="checkbox" id="chkchild3" runat="server" value="Child 3" />
    </li>
    <li>
        <input type="checkbox" id="chkchild4" runat="server" value="Child 4" />
    </li>                                
</div>

请参阅JSFiddle Demo

【讨论】:

  • 您的解释非常棒且 100% 正确,但您没有对父复选框选择做任何事情(对于第 1 节 chkparent1 和第 2 节 chkparent2)
  • 设置 input type=checkbox 和 runat=server 这样服务器端的一切都一样吗?
  • 是的,在大多数情况下。参见 MSDN:“CheckBox Web 服务器控件”和“HtmlInputCheckBox 服务器控件”。
  • 只是想问你一件事,如何取消选中页面加载时通过属性或 document.ready 中的所有复选框?
  • 通过后面代码中的属性:chk1.Checked=false; chk2.Checked = 假;依此类推...在准备好这一行的文档中: var chks = $("form").find(':checkbox'); chks.prop('checked', false);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2011-06-15
  • 2018-11-07
  • 1970-01-01
相关资源
最近更新 更多