开发者

jQuery receiving checkbox status

开发者 https://www.devze.com 2023-04-10 05:32 出处:网络
I\'m changing checkbox status this way: $(this).attr(\"checked\", \'checked\'); After this I want to receive checkbox status, but I got this:

I'm changing checkbox status this way: $(this).attr("checked", 'checked');

After this I want to receive checkbox status, but I got this:

$(this).attr('checked'): "checked"
$(this).is(':checked'): false

How this can be?

P.S. Maybe I'm not correctly changing check开发者_开发知识库box status via jQuery?


The proper way is $(this).prop('checked') to return the boolean property instead of the attribute (which is a a string).

Using .prop() you can also set the checked state: $(this).prop('checked', true_or_false);

As you can see on http://jsfiddle.net/ThiefMaster/QR2fL/, .attr('checked') returns the initial value of the attribute - it does not changed when checking/unchecking the checkbox.


You should not check the checkbox like this:

$(this).attr("checked", 'checked');

but like this

$(this).prop("checked", true);

To check if a checkbox is checked you can use:

$(this).prop('checked');

or

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

which return a boolean proerty

0

精彩评论

暂无评论...
验证码 换一张
取 消