简体   繁体   English

Javascript一个功能验证相同形式的复选框组和单选按钮组

[英]Javascript one function validate same form checkbox group and radio button group

I'm Javascript beginner, writing JS function to validate a radio button group and a checkbox group; 我是Javascript初学者,编写JS函数来验证单选按钮组和复选框组; if either of them are completely unchecked, respective alert(s) show and stop the form submitting. 如果它们中的任何一个都未选中,则显示相应的警报并停止提交表单。

I copied code for radio button group validation, then adjusted it by adding a 2nd 'splitter' function which takes radio and checkbox names as 2 arguments, then sends both names in sequence to the 'validate' function. 我复制了用于单选按钮组验证的代码,然后通过添加第二个“拆分器”函数对其进行了调整,该函数将单选和复选框名称作为2个参数,然后将这两个名称依次发送到“ validate”功能。

This works, but is there more compact way to do this in one function, JS version of select case or something? 这行得通,但是在一个功能,选择大小写的JS版本或其他功能中,是否有更紧凑的方式来做到这一点?

  <script>

  function splitter(mainst, Cst) {
  validate(mainst);
  validate(Cst);
  }

//'unchecked' counts number of unchecked boxes or buttons
//then compares this to total number in the group, and if same
//this means nothing is checked, therefore show alert and return false.
      function validate(grpname) {
      var unchecked = 0, selectgroup=document.getElementsByName(grpname)
      for(i=0;i<selectgroup.length;i++) {
      if(selectgroup.item(i).checked == false) {
      unchecked++;
      }
      }
      if(unchecked == selectgroup.length) {
        if (grpname == 'mainstreet') {
        alert("Please select a main street");
        return false;
        }
        else {
        alert("Please select your C street(s)");
        return false;
        }
      } else {
      return true;
      }
      }

      </script>

Example form here: 此处的示例表格:

  <form action="" method="post" onsubmit="return splitter('mainstreet', 'Cstreet')">
  Street:
  <input type="radio" name="mainstreet" value="jones"> jones street
  <input type="radio" name="mainstreet" value="bones"> bones street
  <input type="radio" name="mainstreet" value="drones"> drones street
  <input type="radio" name="mainstreet" value="foobar"> foobar street
  <br /><br />
  Side street:
  <input type="checkbox" name="Cstreet" value="barfoo"> barfoo street
  <input type="checkbox" name="Cstreet" value="candoo"> candoo street
  <input type="checkbox" name="Cstreet" value="cantdo"> cantdo street
  <input type="checkbox" name="Cstreet" value="canoe"> canoe street
  <input type="checkbox" name="Cstreet" value="wahoo"> wahoo street

  <input type="submit" value="Submit" />
  </form>
<script>
function splitter(mainst, Cst) {
    if(document.querySelector('input[name="'+mainst+'"]:checked') == null ){
        alert("Please select a main street");
        return false;
    }
    else if (document.querySelector('input[name="'+Cst+'"]:checked') == null) {
        alert("Please select your C street(s)");
        return false;
    }
 }
</script>

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

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