Javascript 使用jquery检查复选框是否被选中

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

check whether checkbox is checked or not using jquery

javascriptjqueryhtmlcheckboxchecked

提问by gautamlakum

I want to check whether checkbox is checked or not using jquery. I want to check it on checkbox's onclick event.

我想使用 jquery 检查复选框是否被选中。我想在复选框的 onclick 事件上检查它。

<input type="checkbox" onclick="javascript:check_action();" id="Public(Web)" checked="checked" value="anyone" name="data[anyone]">

Is it possible? How?

是否可以?如何?

Thanks.

谢谢。

回答by Tim Down

First, don't use javascript:in event handler attributes. It's wrong and only works because it happens to be valid JavaScript syntax. Second, your idis not valid. Parentheses are not allowed in the idattribute (in HTML 4 at least; HTML 5 lifts this restriction). Third, if you're using jQuery it probably makes sense to use its click()method to handle the clickevent, although be aware that changing it to do that will mean that if the user clicks on the checkbox before the document has loaded then your script won't handle it.

首先,不要javascript:在事件处理程序属性中使用。这是错误的,并且只因为它是有效的 JavaScript 语法才有效。其次,你id的无效。id属性中不允许使用括号(至少在 HTML 4 中;HTML 5 取消了此限制)。第三,如果您使用的是 jQuery,则使用它的click()方法来处理click事件可能是有意义的,但请注意,将其更改为这样做将意味着如果用户在文档加载之前单击复选框,那么您的脚本将不会处理它。

<input type="checkbox" id="Public_Web" checked value="anyone"
    name="data[anyone]">

$(document).ready(function() {
    $("#Public_Web").click(function() {
        if (this.checked) {
            alert("Checked!");
        }
    });
});

回答by Edson Medina

You can also use this

你也可以用这个

if($("#checkbox_id").is(':checked'))

回答by Sarfraz

You can do like this

你可以这样做

$('#checkbox_id').click(function(){
  alert(this.checked);
});

Or using is()method:

或使用is()方法:

$('#checkbox_id').click(function(){
  if ($(this).is(':checked')){
    alert('Checked');
  }
  else{
    alert('Not Checked');
  }
});

If you want to do this for all checkbox inside a form, you can use the :checkboxfilter selector like this:

如果要对表单中的所有复选框执行此操作,可以使用如下:checkbox过滤器选择器:

$('#form_id :checkbox').click(function(){
  alert(this.checked);
});

Make sure to wrap your code in ready handler:

确保将您的代码包装在就绪处理程序中:

$(function(){
  // code....
});

回答by Amit Maru

There are Several options are there like....

有几种选择,例如......

1. if($("#ans").is('checked')) 
 2. if($("#ans:checked"))
 3. if($('input:checkbox:checked')) 

回答by Prabhagaran

Try This:

尝试这个:

if(!$('#elementid').is(':checked')){
    alert('Not checked');
}else{
    alert('checked');
}

回答by Faisal

You can do this using is()method:

您可以使用以下is()方法执行此操作:

$('##Public_Web').click(function(){
  if ($(this).is(':checked')){
    alert('Checked');
  }
  else{
    alert('Not Checked');
  }
});

If you want to do this for all checkbox inside a form, you can use the :checkboxfilter selector:

如果要对表单中的所有复选框执行此操作,可以使用:checkbox过滤器选择器:

$('#form_id :checkbox').click(function(){
  alert(this.checked);
});

Make sure to wrap your code in document ready handler:

确保将您的代码包装在文档就绪处理程序中:

 $(document).ready(function() {
      // code....
  });

回答by lonesomeday

Firstly, bind the event in script tags, rather than inline. This is much easier to follow and makes your HTML far more readable. Then you can use the jQuery selector :checkedto determine whether the checkbox is checked.Then you can use the checkedattribute of the element.

首先,在脚本标签中绑定事件,而不是内联。这更容易遵循,并使您的 HTML 更具可读性。 然后就可以使用 jQuery 选择器:checked来判断复选框是否被选中。然后就可以使用checked元素的属性了。

$(document).ready(function(){
    $('#Public(Web)').click(function(){
        if (this.checked) {
            // do your code here
        }
    });
});

回答by Hannes

Here is another example, notice instead of using a onClick event in the Element itself i would prefer to use a listener, but remove the brackets from the id, It will give you a hard time in some browsers.

这是另一个示例,请注意,与其在 Element 本身中使用 onClick 事件,我更喜欢使用侦听器,但从 id 中删除括号,这会在某些浏览器中给您带来困难。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<input type="checkbox" id="PublicWeb" checked="checked" value="anyone" name="data[anyone]" />
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready(function(){
    $('#PublicWeb').click(function(){
        if($(this).is(':checked')){
            alert("its checked alright");
        }
    });
});
</script>
</head><body></body></html>

回答by Hannes

html

html

 <input type="checkbox" id="chkBox" checked/> Change Check Box

jquery

查询

$("#chkBox").change(function() {
     if (this.checked) {
         alert("Checked");
         }
         else {
             alert("Not Checked")
             }
           });

 $("#chkBox").change(function() {
 if (this.checked) {
     alert("Checked");
     }
     else {
         alert("Not Checked")
         }
       });
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="chkBox" checked/> Change Check Box