admin管理员组

文章数量:1202780

Is there a quick way or function that would tell me true/false if all check boxes are deselected? Without going through array? (with JS and HTML)

All my check boxes have the same name...

<form action="/cgi-bin/Lib.exe" method=POST name="checks" ID="Form2">
    <input type=checkbox name="us" value="Joe" ID="Checkbox1">
    <input type=checkbox name="us" value="Dan" ID="Checkbox2">
    <input type=checkbox name="us" value="Sal" ID="Checkbox3">
</form>

Is there a quick way or function that would tell me true/false if all check boxes are deselected? Without going through array? (with JS and HTML)

All my check boxes have the same name...

<form action="/cgi-bin/Lib.exe" method=POST name="checks" ID="Form2">
    <input type=checkbox name="us" value="Joe" ID="Checkbox1">
    <input type=checkbox name="us" value="Dan" ID="Checkbox2">
    <input type=checkbox name="us" value="Sal" ID="Checkbox3">
</form>
Share Improve this question asked May 28, 2009 at 21:56 T.T.T.T.T.T. 34.5k47 gold badges134 silver badges172 bronze badges
Add a comment  | 

8 Answers 8

Reset to default 9

jQuery would be a mass of unneeded bloat for a task this trivial. Consider using it if you are running it for other purposes, but all you need is something like this:

function AreAnyCheckboxesChecked () {
  var checkboxes = document.forms.Form2.elements.us;
  for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].checked) {
      return true;
    }
  }
  return false;
}

You have to loop through them. Even a library like jQuery will loop through them, just hide it from you.

var form = document.getElementById('Form2');
var inputs = form.getElementsByTagName('input');
var is_checked = false;
for(var x = 0; x < inputs.length; x++) {
    if(inputs[x].type == 'checkbox' && inputs[x].name == 'us') {
        is_checked = inputs[x].checked;
        if(is_checked) break;
    }
}
// is_checked will be boolean 'true' if any are checked at this point.

JavaScript:

var allischecked = (function(){
  var o = document.getElementById("Form2").getElementsByTagName("input");
  for(var i=0,l=o.length;i<l;i++){
    o[i].type === "checkbox" && o[i].name === "us" && o[i].checked || return false;
  }
  return true;
})();

With jQuery:

var allischecked = ($("#Form2 input:checkbox:not(checked)").length === 0);

In summary, this snipped will return true if all are NOT checked. It bails out as soon as a checked one is found.

var a = document.getElementsByName("us");
for(var i=0; i<a.length; i++)
   if(a[i].checked)
      return false;
return true;

(did not test, but conceptually it is valid)

What do you mean by

Without going through array

?

You could just do

 function check() {
    var anyChecked = false;
    var form = document.getElementById('Form2');
    var checkboxes = form.getElementsByTagName('input');
    for(var i=0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                anyChecked  = true;
                break;
            }
    } 
    alert("Checkboxes checked? " + anyChecked);
}

Working Demo

If you have a large amount of checkboxes that you don't want to loop through to test it might be more efficient to use this approach.

var checked = 0;

$("input[type=checkbox]").live("click", function() {
    if($(this).attr("checked")) checked++;
    else checked--;
}

Then you would be able to test like this.

if(checked === 0) {
    doSomething();
}

The proper solution with jQuery attribute checked:

$checkboxes = $('#Form2 input:checkbox');
$checkboxes.on('click', checkboxes);

function checkboxes() {
  var allChecked = $checkboxes.not(':checked').length == 0;
  console.log(allChecked);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form action="/cgi-bin/Lib.exe" method=POST name="checks" ID="Form2">
    <input type=checkbox name="us1" value="Joe" ID="Checkbox1"><label>Joe</>
    <input type=checkbox name="us2" value="Dan" ID="Checkbox2"><label>Dan</>
    <input type=checkbox name="us3" value="Sal" ID="Checkbox3"><label>Sal</>
</form>

Even easier without loop

const toggleCheckboxes = checkbox => {
   if(checkbox.checked){
      return true
   }else{
      if(document.querySelectorAll(':checked').length === 0){
          // All are unchecked
          return false
      }
   }
}

本文标签: javascriptFast way to validate if all checkboxes are unselectedStack Overflow