jQuery 如果未选中复选框问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4804342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:57:29  来源:igfitidea点击:

jQuery if checkbox is not checked issue

jquerycheckbox

提问by Adi

Possible Duplicate:
Check checkbox checked property using jQuery

可能的重复:
使用 jQuery 检查复选框已选中的属性

I am having an issue picking up when a checkbox is not checked.

我在未选中复选框时遇到问题。

Here is my sample code:

这是我的示例代码:

$(".get-the-look ul input").click(function(){
    if("this:checked") {
        var product = $(this).attr('alt');
        $("#bob div").remove('.'+product);
    } else {
        alert('asdasd');
    }
});

But the alert (if the checkbox isn't checked) never fires... The 'else' never kicks in no matter what state the checkbox is in?

但是警报(如果未选中复选框)永远不会触发......无论复选框处于什么状态,“其他”永远不会启动?

I don't see where I am going wrong.

我不明白我哪里出错了。

采纳答案by Tom Tu

you can't pass this:checkedas a string - it won't work as it will be interpreted as a string and always evaluate to true

你不能this:checked作为一个字符串传递- 它不会工作,因为它会被解释为一个字符串并且总是评估为true

use $(this).is(':checked')instead

使用$(this).is(':checked')替代

回答by jensgram

The string "this:checked"will always evaluate to TRUE. Try this.checkedinstead, i.e., evaluate the property checkedon this:

该字符串"this:checked"将始终评估为TRUE。尝试this.checked代替,即评估财产checkedthis

if(this.checked) { ...