简体   繁体   English

检查是否选中了特定的复选框

[英]Check if specific checkboxes are checked

I have a form with multiple checkboxes like this: 我有一个带有多个复选框的表单,如下所示:

<form id="myForm">
   <input type="checkbox" id="first" />
   <input type="checkbox" id="second" />
   <input type="checkbox" id="third" />
</form>

I want to do something (ie alert box) only when checkbox 1 and 3 are selected. 我仅在选择复选框1和3时才想做某事(即警报框)。

I know how to target one check box like this: 我知道如何定位一个复选框,如下所示:

$('#first').click(function () {
    if ($(this).prop("checked") == true) {
        alert("I'm checked!");
    }
});

But is there a way to do that for specific multiple checkboxes? 但是有没有办法针对特定的多个复选框呢?

This answer works on the premise that you want to when either of the specified check-boxes are checked, rather than when they're both checked, this can be done with either comma separated selectors: 此答案的前提是您希望选中两个指定复选框中的任何一个,而不是同时选中两个指定复选框,而可以使用两个逗号分隔的选择器来完成:

$('#first, #third')

Or through the use of a class-name to identify the relevant elements: 或通过使用类名来标识相关元素:

$('.alertIfSelectedClassName')

With the HTML of: 使用以下HTML:

<form id="myForm">
  <input type="checkbox" class="alertIfSelectedClassName" id="first" />
  <input type="checkbox" id="second" />
  <input type="checkbox" class="alertIfSelectedClassName" id="third" />
</form>

The use of CSS attribute selection,nbased on a shared attribute, such as the name of the <input> : 基于共享属性(例如<input>的名称)使用CSS属性选择:

$('input[name=alertIfSelectedElementName')

HTML: HTML:

<form id="myForm">
  <input type="checkbox" name="alertIfSelectedElementName" id="first" />
  <input type="checkbox" id="second" />
  <input type="checkbox" name="alertIfSelectedElementName" id="third" />
</form>

You can iterate through them: 您可以遍历它们:

<form id="myForm">
    <input type="checkbox" name="somename" id="first" />
    <input type="checkbox" name="somename" id="second" />
    <input type="checkbox" name="somename" id="third" />
</form>

$('checkbox[name=somename"]').each(function () {
    if ($(this).prop("checked") == true) {
        alert("I'm checked!");
    }
});

Try this: 尝试这个:

      $('#first, #third').click(function () {
          var firstChecked = $('#first).prop("checked") == true);
          var thirdChecked = $('#third).prop("checked") == true);
          if (firstChecked  && thirdChecked ) {
             alert("I'm checked!");
          }
       })

https://jsfiddle.net/p1zjaw46/ https://jsfiddle.net/p1zjaw46/

var $toCheck = $('#first').add('#third');
$(function () {
  $('input[type="checkbox"]').on('change', function () {


    if ( $toCheck.filter(':checked').length === $toCheck.length )
      alert('good');
    else
      alert('bad');
  })
});

Basically: 基本上:

  1. cache the items you want to check into a var. 将要检查的项目缓存到var中。 $toCheck in this case. $toCheck在这种情况下。
  2. When any are changed , do a quick check. 如果有任何更改 ,请进行快速检查。 You filter by ":checked" , and compare the length of the results of the filter to the length of the original set of checkboxes. 可以通过":checked"进行filter ,并将filter结果的长度与原始复选框的长度进行比较。

If they aren't equal, at least one of the expected boxes aren't checked! 如果它们不相等,则不会选中至少一个预期的框!

Why is this answer better than others? 为什么这个答案比其他答案更好?

It's scalable 可扩展

All you need to do is add new elements to $toCheck and the rest is automatic 您需要做的就是将新元素添加到$toCheck ,其余的都是自动的

You can check if the checkbox is checked using is(':checked') , you can check the if the both are checked using && sign check example bellow. 您可以使用is(':checked')来检查复选框是否已选中,也可以使用&&符号检查示例来检查是否都选中了复选框。

Hope this helps. 希望这可以帮助。


 $("#first, #third").click(function () { if ($('#first').is(':checked') && $('#third').is(':checked')) { alert("Message!"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="myForm"> <input type="checkbox" id="first" /> <input type="checkbox" id="second" /> <input type="checkbox" id="third" /> </form> 

In order to target a specific elements, try using a css class as a selector. 为了定位特定元素,请尝试使用css类作为选择器。 Also, include the input type so that you don't have any conflict with other elements having the same class. 另外,包括输入type以使您与具有相同类的其他元素没有任何冲突。 In order to check if all the required checkboxes are checked or not, you can use .each() loop and determine if they are all checked and based on that display the alert() . 为了检查是否选中了所有必需的复选框,可以使用.each()循环并确定是否都选中了这些复选框,并基于显示的alert()来确定它们。 The following approach avoids any hardcoding and multiple if checks. 以下方法避免了任何硬编码和multiple if检查。

$("input:checkbox.test").click(function()
{
    var allchecked = true;
    $("input:checkbox.test").each(function(){
        if(!$(this).is(":checked")) 
           allchecked = false;         
    });

    if(allchecked)
      alert("Im checked");
});

Working example : https://jsfiddle.net/DinoMyte/fy1asfws/34/ 工作示例: https : //jsfiddle.net/DinoMyte/fy1asfws/34/

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

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