开发者

How to tell whether a checkbox is checked?

开发者 https://www.devze.com 2023-02-09 11:11 出处:网络
As most people know there are at least two (easy) ways to findout if a checkbox is checked. ($(\'#checkbox:checked\').val() != undefined) OR ($(\'#c开发者_StackOverflow中文版heckbox\').val() == \'on\

As most people know there are at least two (easy) ways to findout if a checkbox is checked.

($('#checkbox:checked').val() != undefined) OR ($('#c开发者_StackOverflow中文版heckbox').val() == 'on')

Which of these two methods is best? why?


I think that

$('#checkbox').is(':checked');

is the way to go.


If you're accessing the raw element, you should be able to use something like:

this.checked

But I don't know if there might be any cross-browser compatibility issues with this method.


Another way

if ($('#myCheckbox').attr("checked")) { .. }

Anyways...I don't think any one of these is necessarily better or worse. jQuery is designed to work across browsers so any one of them should work. Not sure anybody has taken the time to actually benchmark them for performance or anything...just do what you feel is most readable to you.

0

精彩评论

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