asp.net Jquery checkall/uncheck all checkboxes

I have a LinkButton (below) name "Insert?", it triggers the Jquery to check all checkboxes within my DataList1. It works so far.

But I like to click on the same LinkButton and it will uncheck all checkboxes previously checked. Basically, it will toggle between checkall and uncheck all.

<asp:LinkButton ID="ENameLinkBtn" runat="server"  OnClientClick="javascript:SelectAllCheckboxes1(this)" >Insert?</asp:LinkButton>

Jquery that works only checkAll, but not uncheckAll

<script type="text/javascript">
    function SelectAllCheckboxes1(chk) {
        $('#<%=DataList1.ClientID%>').find("input:checkbox").each(function () {
            //alert(this.id);
            this.checked = true; 
        });
    }
</script>

Please help. Thanks,


Since it is a link, what you can do is

function SelectAllCheckboxes1(chk) {
    var $checks = $('#<%=DataList1.ClientID%>').find("input:checkbox");
    $checks.prop('checked', $checks.not(':checked').length != 0);
}

Demo: Fiddle


Considering you have a jQuery tag in your question, I assume you can just use this command on click of your ID element. You don't need any ASP.NET server side action for this.

http://jsfiddle.net/879Pv/1/

$("#ENameLinkBtn").click(function(){
   $(document.body).find(':checked').each(function() {
      $(this).removeAttr('checked');
   });
});
链接地址: http://www.djcxy.com/p/71038.html

上一篇: 勾选复选框或不使用jquery

下一篇: asp.net jquery checkall /取消选中所有复选框