对于复选框的JavaScript验证,当从选项列表中选择多个复选框时

时间:2014-10-15 08:07:09

标签: javascript html arrays checkbox listbox

我想为Checkbox进行JavaScript验证

如果未选中“复选框中的值”,则应提示“请选择您知道的语言”

但是如果用户从复选框列表中选择2种语言。我需要使用JavaScript来提醒2种语言。

我知道单一复选框,但没有得到如何使用数组。

这是我的代码......

<script type="text/javascript">
 
function myFunction(form){
    
  var i,
  chks = document.getElementsByName('lang[]');
  
  for (i = 0; i < chks.length; i++){
    if (chks[i].checked){
        
      //Here how i should alert selected values 
        
        return true;
    }else{
        alert('No item selected');
        return false;
    }
  }
}
</script>
 <form name="registration" id="registration_form_id" action="" method="post" onsubmit="return myFunction(this)">

      <div id="languageId"><label>Language : </label>
                                                        
          <input type="checkbox" name="lang[]" value="english" >English</input>
                                        
          <input type="checkbox" name="lang[]" value="marathi">Marathi</input>   

           <input type="checkbox" name="lang[]" value="hindi">Hindi</input> 
                                                    
      </div>

     <div id="submit1"><button type="submit">Submit</button></div><br/>

     </form>

谢谢你, 拉胡

2 个答案:

答案 0 :(得分:1)

如果您想获取所选复选框值的列表,您可以使用此..

$('#someButton').click(function() {
    var values= [];
    $('#MyDiv input:checked').each(function() {
        values.push(this.val);
    });
    // now values contains all of the values of checked checkboxes
    // do something with it
});

答案 1 :(得分:0)

如果您只需要一个选定的值,则使用radio输入类型会更容易。有了它,您可以使用querySelector找到所选值(请参阅MDN)。对于多种语言,您还可以使用querySelectorAll,将其结果转换为Array并使用Array.map(请参阅MDN)将结果映射到Array已选中复选框的值。

这里针对这两种情况(没有表单)重写了您的代码段:

&#13;
&#13;
(function (d, w, undefined) {
  d.querySelector('#languageId button').addEventListener('click', doSubmit);
  d.querySelector('#multiLanguageId button').addEventListener('click', doSubmitMulti);
  var languageSelectorContainer = d.querySelector('#languageId');
  var multiLanguageSelectorContainer = d.querySelector('#multiLanguageId');
  
  function doSubmit() {
    var languageChecked = languageSelectorContainer
                           .querySelector('[type=radio]:checked');
    d.querySelector('#result').innerHTML = 
                    'your selected language: <b>'+ 
                     (languageChecked 
                      ? languageChecked.value 
                      : 'not yet selected') +
                     '</b>';
  }
  
  
  function doSubmitMulti() {
    var languagesChecked = [].slice.call( 
                              multiLanguageSelectorContainer
                                .querySelectorAll('[type=checkbox]:checked') )
                            .map(function (v){
                              return v.value;
                             });
    d.querySelector('#result').innerHTML = 
                    'you selected these language(s): <b>'+ 
                     (languagesChecked.length 
                      ? languagesChecked.join(', ') 
                      : 'none yet selected') +
                     '</b>';
  }
}(document, window))
&#13;
<div id="languageId">Pick your language:
  <input type="radio" name="lang[]" value="english">English
  <input type="radio" name="lang[]" value="marathi">Marathi
  <input type="radio" name="lang[]" value="hindi">Hindi
  <button>Submit</button>
</div>

<div id="multiLanguageId">Pick the languages you know:
  <input type="checkbox" name="mlang[]" value="english">English
  <input type="checkbox" name="mlang[]" value="marathi">Marathi
  <input type="checkbox" name="mlang[]" value="hindi">Hindi
  <button>Submit</button>
</div>

<div id="result"></div>
&#13;
&#13;
&#13;