2016-08-17 39 views
-1

我遇到问题触发服务器端按钮,我想要做类似于this但具有服务器端按钮和复选框。它目前不工作,我不明白为什么?单击服务器端按钮和使用JQuery选择/取消选择Chechboxes

$(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>

回答

0

你缺少#在你的代码id选择了btnSelect。

$(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"); 
     }  
    }); 

});