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.
精彩评论