简体   繁体   English

使用jQuery检查是否选中了复选框

[英]check whether checkbox is checked or not using jquery

I want to check whether checkbox is checked or not using jquery. 我想检查是否已使用jquery选中了复选框。 I want to check it on checkbox's onclick event. 我想在复选框的onclick事件上对其进行检查。

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

Is it possible? 可能吗? How? 怎么样?

Thanks. 谢谢。

First, don't use javascript: in event handler attributes. 首先,不要在事件处理程序属性中使用javascript: It's wrong and only works because it happens to be valid JavaScript syntax. 这是错误的,仅可使用,因为它恰好是有效的JavaScript语法。 Second, your id is not valid. 其次,您的id无效。 Parentheses are not allowed in the id attribute (in HTML 4 at least; HTML 5 lifts this restriction). id属性中不允许使用括号(至少在HTML 4中; HTML 5取消了此限制)。 Third, if you're using jQuery it probably makes sense to use its click() method to handle the click event, 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. 第三,如果您使用的是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!");
        }
    });
});

你也可以用这个

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

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 :checkbox filter selector like this: 如果要对表单中的所有复选框执行此操作,可以使用:checkbox过滤器选择器,如下所示:

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

Make sure to wrap your code in ready handler: 确保将代码包装在就绪的处理程序中:

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

There are Several options are there like.... 有几种选择....

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

Try This: 尝试这个:

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

Firstly, bind the event in script tags, rather than inline. 首先,将事件绑定在脚本标签中,而不是内联。 This is much easier to follow and makes your HTML far more readable. 这很容易遵循,并使您的HTML更具可读性。 Then you can use the jQuery selector :checked to determine whether the checkbox is checked. 然后,您可以使用jQuery选择器:checked确定该复选框是否已选中。 Then you can use the checked attribute of the element. 然后,您可以使用元素的checked属性。

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

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 :checkbox filter selector: 如果要对表单中的所有复选框执行此操作,可以使用:checkbox过滤器选择器:

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

Make sure to wrap your code in document ready handler: 确保将代码包装在文档就绪处理程序中:

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

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>

html html

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

jquery 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM