Check ASP.NET checkbox with jQuery

I have the following ASP.NET server control flags.

<asp:checkbox id="chkMonS1" runat="server" />
<asp:checkbox id="chkMonS2" runat="server" />
<asp:checkbox id="chkAllMon" runat="server" />

As well as other flags.

<asp:checkbox id="chkTueS1" runat="server" />
<asp:checkbox id="chkTueS2" runat="server" />
<asp:checkbox id="chkAllTue" runat="server" />

So, I tried using jQuery to select all the checkboxes, for example.

<script type="text/javascript">
        $('chkAllMon').click(
        function () {
            $('chkMonS1').attr('checked', true)
            $('chkMonS2').attr('checked', true)
        }
        )
    </script>

However, this will not work. Where did this happen?

+5
source share
5 answers

If you are using ASP.NET 4.0 or later, this is easy to fix.

First, you need a symbol #to your selections, the jQuery, for example $('#chkMonS1').

In addition, you need to install ClientIdMode="Static"in ASP.NET elements as shown below. This sets the HTML idto the same value as ASP.NET id. See this article for this reason.

<asp:checkbox id="chkAllTue" runat="server" ClientIDMode="Static" />

+8
source

. :

$('#<%=chkAllMon.ClientID %>').click(
        function () {
            $('#<%=chkMonS1.ClientID %>').attr('checked', true)
            $('#<%=chkMonS2.ClientID %>').attr('checked', true)
        }

: ASP.NET - , . ASP.NET html, .

+9

( ..), jQuery . , :

+1

ASP.NET 2.0, ID:

<script type="text/javascript">
    $('#<%=chkAllMon.ClientID%>').click(
    function () {
        $('#<%=chkMonS1.ClientID%>').attr('checked', true)
        $('#<%=chkMonS2.ClientID%>').attr('checked', true)
    }
    )
</script>

ASP.NET 4.0, . ChessWiz.

+1

id:

$('#chkAllMon').click(function () {
    $('#chkMonS1').attr('checked', true)
    $('#chkMonS2').attr('checked', true)
});
0

All Articles