gpt4 book ai didi

javascript - jquery 如何获取选中复选框的值?

转载 作者:行者123 更新时间:2023-12-01 04:02:43 26 4
gpt4 key购买 nike

嗨,我正在使用 asp.net 开发 jquery 应用程序。我有如下复选框列表。如果我选中任何复选框,那么我想显示它。下面是我的复选框列表。

  <asp:CheckBoxList ID="ChKResons" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" Style="margin-bottom: 8px; margin-right: 5px;" CellPadding="5" CellSpacing="25">
<asp:ListItem Text="Your financial offer exceeded our budgeted amount" value="1"/>
<asp:ListItem Text="Your technical offer didn't comply with Oman Air's technical requirement" Value="2" />
</asp:CheckBoxList>

这是我的 jquery 代码。

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

上面的代码不起作用。我可以得到一些帮助来解决这个问题吗?如果有帮助,我们将不胜感激。谢谢。

最佳答案

使用

$(this).prop('checked') 

而不是

$(this).is(':checked')

如果您生成的 HTML 复选框具有 ID ChKResons,那么请仅使用 ID 添加事件,例如:

$('#ChKResons').on('click', function () {
alert($(this).prop('checked'));
});

仅附加事件一次。如果您有多个具有相同 id 的元素,则删除 id 并在所有元素上添加一个公共(public)类,并在类选择器上附加一个事件。

$('.commonClass').on('click', function () {
alert($(this).prop('checked'));
});

关于javascript - jquery 如何获取选中复选框的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com