checkbox val not showing false

2019-07-17 03:48发布

I have the follwoing code:

 $("input:checkbox").live('click', function() {
                          alert($(this).val());
});

True is shown if the checkbox is checked. However, if the checkbox is checked and I uncheck it, the value shown is still true. How can I correct that? What is wrong with the code? Thanks

2条回答
smile是对你的礼貌
2楼-- · 2019-07-17 04:26
this.checked //true/false - not jquery
$(element).checked //does not work
$(element).attr('checked') //checked/NULL *
$(element).is(':checked') //true/false

*note that <input TYPE=CHECKBOX CHECKED=CHECKED> returns in both checked/unchecked state checked

查看更多
萌系小妹纸
3楼-- · 2019-07-17 04:32

The value of a checkbox is always the same. You need to see if it is checked or not.

….attr('checked');
查看更多
登录 后发表回答