简体   繁体   English

通过 jquery 获取 asp.net CheckBox 的值

[英]Get value of asp.net CheckBox via jquery

I'm trying to load up a hidden field with a list of values from all checkboxes that are checked.我正在尝试加载一个隐藏字段,其中包含所有选中的复选框的值列表。 Everything works fine except that the is(':checked') always returns false.一切正常,除了 is(':checked') 总是返回 false。 Any ideas?有任何想法吗?

            $('.invCheckBox').click(function () {
            var cb = '';
            var i = 0;
            debugger;
            $(".invCheckBox").each(function (index, element) {
                debugger;
                if ($(element).is(':checked')) {
                    alert($(element).attr('invNbr'));
                    if (i == 0) {
                        cb = $(element).attr('invNbr')
                    }
                    else {
                        cb = cb + ',' + $(element).attr('invNbr');
                    };
                    i = i + 1;

                };
            });
            $('MainContent_txtHiddenField').text(cb);
        });

Here is a fiddle for you might help :) http://jsfiddle.net/sAyfw/这是一个可能对您有所帮助的小提琴 :) http://jsfiddle.net/sAyfw/

$('.invCheckBox').change(function () { $('.invCheckBox').change(function () {

$('.invCheckBox').each(function()
                       {
                           alert($(this).is(':checked'));
                       });
    });

For whatever reason creating a CheckBox control server-side wraps the input tag in a span tag.无论出于何种原因,创建 CheckBox 控件服务器端都会将输入标记包装在 span 标记中。 Weird.奇怪的。 So, I changed the code to create an HtmlInputCheckBox instead and now the jQuery works beautifully.因此,我更改了代码以创建一个 HtmlInputCheckBox,现在 jQuery 工作得很好。

            Dim divCheckBox As New HtmlGenericControl("div")
        divCheckBox.Attributes.Add("style", "width: 95px; float: left;")
        Dim chkCheckBox As New HtmlInputCheckBox
        chkCheckBox.Attributes.Add("class", "invCheckBox")
        chkCheckBox.Attributes.Add("invNbr", invoice.InvoiceNbr)
        divCheckBox.Controls.Add(chkCheckBox)
        divInvRow.Controls.Add(divCheckBox)

Thanks for the info about asp:CheckBox being wrapped in a span.感谢有关 asp:CheckBox 被包装在一个跨度中的信息。 My jQuery was always returning false, and I couldn't figure out why it was not working.我的 jQuery 总是返回 false,我不知道为什么它不起作用。

I used this:我用过这个:

console.log($(this).children(1).is(':checked')); console.log($(this).children(1).is(':checked'));

and that works perfectly with an asp:CheckBox.这与 asp:CheckBox 完美配合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM