检查复选框是否在单击时未选中 - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11159221/
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 10:15:58  来源:igfitidea点击:

Check if checkbox is NOT checked on click - jQuery

jquery

提问by w00

I want to check if a checkbox just got unchecked, when a user clicks on it. The reason for this is because i want to do a validation when a user unchecks a checkbox. Because atleast one checkbox needs to be checked. So if he unchecks the last one, then it automatically checks itself again.

我想检查一个复选框是否刚刚被取消选中,当用户点击它时。这样做的原因是因为我想在用户取消选中复选框时进行验证。因为至少需要选中一个复选框。所以如果他取消选中最后一个,那么它会自动再次检查自己。

With jQuery i can easily find out wether it's checked or not:

使用 jQuery 我可以很容易地找出它是否被检查:

$('#check1').click(function() {
    if($(this).is(':checked'))
        alert('checked');
    else
        alert('unchecked');
});

But i actually only want to have an if statement that checks if a checkbox just got unchecked.

但我实际上只想有一个 if 语句来检查复选框是否刚刚被取消选中。

So i thought i could do that with the following code:

所以我想我可以用下面的代码做到这一点:

$('#check2').click(function() {
    if($(this).not(':checked'))
        alert('unchecked');
    else
        alert('checked');
});

But this will always show the 'unchecked' message. Not really what i was expecting...

但这将始终显示“未选中”消息。真的不是我所期待的......

demo:http://jsfiddle.net/tVM5H/

演示:http : //jsfiddle.net/tVM5H/

So eventually i need something like:

所以最终我需要这样的东西:

$('#check2').click(function() {
    if($(this).not(':checked')) {
        // Got unchecked, so something!!!
    }
});

But obviously this doesn't work. I rather don't want to use the first example, because then i'd have an unnecessary 'else' statement when i only need one 'if' statement.

但显然这行不通。我宁愿不想使用第一个示例,因为当我只需要一个“if”语句时,我就会有一个不必要的“else”语句。

So first thing, is this a jQuery bug? Cause to me it's unexpected behaviour. And second, anyone any ides for a good alternative?

那么首先,这是一个 jQuery 错误吗?因为对我来说这是意外的行为。其次,有人有什么好的选择吗?

回答by antyrat

Try this:

尝试这个:

if(!$(this).is(':checked'))

demo

演示

回答by chris_code

The answer already posted will work. If you want to use the jQuery :not you can do this:

已经发布的答案将起作用。如果你想使用 jQuery :not 你可以这样做:

if ($(this).is(':not(:checked)'))

or

或者

if ($(this).attr('checked') == false)

回答by TheZ

jQuery to check for checked? Really?

jQuery 检查检查?真的吗?

if(!this.checked) {

Don't use a bazooka to do a razor's job.

不要使用火箭筒来完成剃须刀的工作。

回答by Nanhe Kumar

$(document).ready(function() {
        $("#check1").click(function() {
            var checked = $(this).is(':checked');
            if (checked) {
                alert('checked');
            } else {
                alert('unchecked');
            }
        });
    });

回答by Chandresh

<script type="text/javascript">

 if(jQuery('input[id=input_id]').is(':checked')){
  // Your Statment
 }else{
  // Your Statment
 }

 OR

 if(jQuery('input[name=input_name]').is(':checked')){
  // Your Statment
 }else{
  // Your Statment
 }

</script>

Code taken from here : http://chandreshrana.blogspot.in/2015/10/how-to-check-if-checkbox-is-checked-or.html

代码取自此处:http: //chandreshrana.blogspot.in/2015/10/how-to-check-if-checkbox-is-checked-or.html

回答by Nathan Friend

Check out some of the answers to this question - I think it might apply to yours:

查看这个问题的一些答案 - 我认为它可能适用于你的:

how to run click function after default behaviour of a element

如何在元素的默认行为后运行单击功能

I think you're running into an inconsistency in the browser implementation of the onclickfunction. Some choose to toggle the checkbox before the event is fired and some after.

我认为您在该onclick功能的浏览器实现中遇到了不一致的问题。有些选择在触发事件之前和之后触发复选框。

回答by Praveen04

The Answer already posted .But We can use the jquery in this way also

答案已经发布。但是我们也可以通过这种方式使用 jquery

demo

演示

$(function(){
    $('#check1').click(function() {
        if($('#check1').attr('checked'))
            alert('checked');
        else
            alert('unchecked');
    });

    $('#check2').click(function() {
        if(!$('#check2').attr('checked'))
            alert('unchecked');
        else
            alert('checked');
    });
});

回答by Usman Younas

Do it like this

像这样做

if (typeof $(this).attr("checked") == "undefined" )

// To check if checkbox is checked
if( $(this).attr("checked")=="checked")