How can I get checkboxlist currently selected item

2020-07-17 15:55发布

I have a checkboxlist.After when I checked an item on it I will get the selected item value(not all of the selected items values,only the which I'm slected now),How can I do it in jquery.

Here is my code:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" Enabled="False">
        <asp:listitem value="1"></asp:listitem>
        <asp:listitem value="2"></asp:listitem>
        <asp:listitem value="3"></asp:listitem>
        <asp:listitem value="4"></asp:listitem>
    </asp:CheckBoxList>

2条回答
Rolldiameter
2楼-- · 2020-07-17 16:28

You can register a click event handler for all the checkboxes, inside the event handler this will point to the clicked checkbox

if jQuery >= 1.7

$('#CheckBoxList1').on('click', ':checkbox', function() {
    if ($(this).is(':checked')) {
        // handle checkbox check
        alert($(this).val());
    } else {
        // checkbox is unchecked
        alert('unchecked')
    }
});

if jQuery < 1.7

$('#CheckBoxList1 :checkbox').live('click', function() {
    alert($(this).is(':checked'));
});
查看更多
神经病院院长
3楼-- · 2020-07-17 16:47

This will display the value of the checkbox onclick:

$('input:checkbox').click(function() {
    alert($(this).val());
});
查看更多
登录 后发表回答