如何检查多个复选框中是否选中了至少一个复选框?

时间:2015-06-03 07:41:22

标签: javascript checkbox

在我的表单中使用了很多复选框。我想检查是否至少选中了一个复选框。

<div id="sun">
    <li><?php echo $this->__("SUN");?> </li>
    <li><input class="allday" type="checkbox" name="availableday[Sunday][9-12]" value="9AM - 12AM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Sunday][12-6]" value="12PM - 6PM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Sunday][6-9]" value="6PM - 9PM"></li>
</div>
<div id="mon">
    <li><?php echo $this->__("MON");?> </li>
    <li><input class="allday" type="checkbox" name="availableday[Monday][9-12]" value="9AM - 12AM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Monday][12-6]" value="12PM - 6PM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Monday][6-9]" value="6PM - 9PM"></li>
</div>
<div id="tue">
    <li><?php echo $this->__("TUE");?> </li>
    <li><input class="allday" type="checkbox" name="availableday[Tuesday][9-12]" value="9AM - 12AM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Tuesday][12-6]" value="12PM - 6PM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Tuesday][6-9]" value="6PM - 9PM"></li>
</div>
<div id="wed">
    <li><?php echo $this->__("WED");?> </li>
    <li><input class="allday" type="checkbox" name="availableday[Wednesday][9-12]" value="9AM - 12AM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Wednesday][12-6]" value="12PM - 6PM"></li>
    <li><input class="allday" type="checkbox" name="availableday[Wednesday][6-9]" value="6PM - 9PM"></li>
</div>

如何验证至少一个复选框?

6 个答案:

答案 0 :(得分:10)

嗯,直截了当地说:

var isSomeAlldayCheckboxChecked = document
  .querySelectorAll('input[type="checkbox"].allday')
  .some(function (each) { return each.checked; });

答案 1 :(得分:9)

以下几行:

var formNodes  = document.checks.getElementsByTagName('input');
for (var i=0;i<formNodes.length;i++) {
   /* If one is true set a variable to true and exit the loop */
}

堆叠溢出帖子Here

干杯,

Truez

答案 2 :(得分:5)

var count= 0;
var value_array=new Array();
jQuery('input[type="checkbox"]:checked').each(function(){ 
   value_array.push(jQuery(this).val());
   count = count + 1;
});

// count&value_array will tell you total selected

答案 3 :(得分:3)

你可以使用Jquery。

if ($('#form_id :checkbox:checked').length > 0)
{
        // one or more checkboxes are checked
}
else
{
       // no checkboxes are checked
}

答案 4 :(得分:2)

@ herby的答案是检查input[type='checkbox']数组中某些输入是否已被检查的正确答案。

此答案使用 querySelectorAll MyFormManager.get_checkedInputs)返回一系列已检查的输入;它演示了向查询input元素change事件(MyFormManager.init)添加事件侦听器 - 它将检查状态附加到元素的classNameMyFormManager.onCheckChanged) - 促进这一进程。

&#13;
&#13;
var MyFormManager = {
  checkboxes: null,  
  onCheckChanged: function(sender, e) {
    console.log("sender:= %o, e:= %o", sender, e);
    sender.className = sender.className.replace(' true','').replace(' false','');
    sender.className += ' ' + String(sender.checked);
  },
  init : function() {
    checkboxes = document.querySelectorAll("input[type='checkbox']");
    for (var i = 0, max = checkboxes.length; i < max; i++)
    {
      checkboxes[i].addEventListener('change', MyFormManager.onCheckChanged.bind(null, checkboxes[i]));
    }
  },
  get_checkedInputs : function() {
    return document.querySelectorAll("input[type='checkbox'].true");
  }
}

console.log(MyFormManager);
    
MyFormManager.init();

var message = document.querySelector("div#message");
    
document.querySelector("button#runCheck").addEventListener("click", function (e) {
  var checkedInputs = MyFormManager.get_checkedInputs();
  console.log(checkedInputs);
  message.innerHTML = "";
  var parentId;
  for (var i = 0, max = checkedInputs.length; i < max; i++)
  {
    parentId = checkedInputs[i].parentNode.parentNode.parentNode.id;
    message.innerHTML += '<div class="item"><span class="parentId">' + parentId + '</span><span class="checkedInput">' + checkedInputs[i].value + '</span></div>';
  }
});
&#13;
body {background-color:whitesmoke;}

div { display:block;}

div > ul { 
  list-style:none;
  margin:0.5em auto 0 auto;
  padding:0.27em;
  background-color:white;
  border:0.05cm solid silver;
}

input[type='checkbox'] {cursor:pointer;}

div:first-child > ul:first-child {margin-top:0em;}

div > ul > li {list-style:none;}

div > ul > li > label {display:inline-block;padding:0.1em 0.3em;margin:0;width:8em;text-align:right;}


div.item {display:block;}
div.item > span.parentId {display:inline-block;width:3em;text-transform:uppercase;}
div.item > span.checkedInput {display:inline-block;width:8em;text-align:right;}
&#13;
<div id="debugger">
  <div id='message'></div>
  <button id='runCheck'>Run Check</button>
</div>

<div id="sun">
  <ul>
    <li><!-- <?php echo $this->__("SUN");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Sunday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Sunday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Sunday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>

<div id="mon">
  <ul>
    <li><!-- <?php echo $this->__("MON");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Monday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Monday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Monday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>
<div id="tue">
  <ul>
    <li><!-- <?php echo $this->__("TUE");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Tuesday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Tuesday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Tuesday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>
<div id="wed">
  <ul>
    <li><!-- <?php echo $this->__("WED");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Wednesday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Wednesday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Wednesday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>

<div id="thur">
  <ul>
    <li><!-- <?php echo $this->__("THUR");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Thursday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Thursday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Thursday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>

<div id="fri">
  <ul>
    <li><!-- <?php echo $this->__("FRI");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Friday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Friday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Friday][6-9]" value="6PM - 9PM">
    </li>
  </ul>
</div>

<div id="sat">
  <ul>
    <li><!-- <?php echo $this->__("SAT");?> --></li>
    <li>
      <label>9AM - 12PM</label>
      <input class="allday" type="checkbox" name="availableday[Saturday][9-12]" value="9AM - 12PM">
    </li>
    <li>
      <label>12PM - 6PM</label>
      <input class="allday" type="checkbox" name="availableday[Saturday][12-6]" value="12PM - 6PM">
    </li>
    <li>
      <label>6PM - 9PM</label>
      <input class="allday" type="checkbox" name="availableday[Saturday][6-9]" value="6PM - 9PM">
    </li>
  </ul>  
</div>
&#13;
&#13;
&#13;

请注意,这不是优化的,也不考虑动态控件(回发或xhr);

答案 5 :(得分:2)

如果您正在使用supported browser,则可以利用:checked伪选择器进行非常简洁的检查。简单地做这样的事情:

if(document.forms[0].querySelector('input[type="checkbox"]:checked').length) {
  //At least 1 checkbox is selected within document
}

请务必通过文档对象上的document.querySelectordocument.forms列表定位正确的表单(如上所示)。