单击服务器端按钮和 Select/Deselect 使用 JQuery 的复选框

click Server Side Button and Select/Deselect Chechboxes using JQuery

我在触发服务器端按钮时遇到问题,我想做一些类似于 的事情,但有一个服务器端按钮和复选框。它目前无法正常工作,我不明白为什么?

$(function all() {
      $("<%= btnSelect.ClientID %>").click(function () {

        if (document.getElementById("<%= btnSelect.ClientID %>") == 'Select All') {
            $('.button input').prop('checked', true);
            document.getElementById("<%= btnChkboxSelect.ClientID %>").value = "Unselect All";
        } else {
            $('.button input').prop('checked', false);
            document.getElementById("<%= btnChkboxSelect.ClientID %>").value = "Select All";
        }
    });

});
<body>
  <div id="main">
    <div id="first">
     <asp:Button ID="btnSelect" runat="server" UseSubmitBehavior="false" Text="Select All" OnClientClick="all();" />
      <div class="button">
        <div>
          <asp:CheckBox ID="checbox1" runat="server" />
        </div>
        <div>
          <asp:CheckBox ID="checbox2" runat="server" />
        </div>
         <div>
          <asp:CheckBox ID="checbox3" runat="server" />
        </div>
      </div>
</body>

您的代码中缺少 # 作为 btnSelect 的 ID 选择器。

   $(function {
      $("#<%= btnSelect.ClientID %>").click(function () { 
         if($(this).text() == "Select All")
         {
           $('.button input:checkbox').prop('checked', true);
           $(this).text("Unselect All");
         }
         else
         {
           $('.button input:checkbox').prop('checked', false);
           $(this).text("Select All");
         }       
    });

});