Home >Web Front-end >JS Tutorial >How to determine whether a check box is selected in javascript_javascript skills

How to determine whether a check box is selected in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:36:231845browse

Previously I summarized the issue about how jquery determines whether a check box is selected . Today I summarized how to determine whether a js check box is selected
JavaScript determines whether the checkbox is selected. The following is an example (there is now a set of checkboxes as follows):

<input type="checkbox" name="fruit" value="apple" />苹果 
<input type="checkbox" name="fruit" value="orange" />橘子 
<input type="checkbox" name="fruit" value="banana" />香蕉 
<input type="checkbox" name="fruit" value="grape" />葡萄 

The method to get the value of a set of checkboxes with name=fruit is as follows:

<script language="javascript"> 
function checkbox(){ 
  var str=document.getElementsByName('fruit'); 
  var strstrLen=str.length; 
  var checkVal=''; 
  for (i=0;i<strLen;i++){ 
    if(str[i].checked==true){ 
      checkVal+=str[i].value+','; 
    } 
  } 
  if(checkVal==''){ 
    alert("请先选择复选框~!"); 
  }else{ 
    alert("复选框的值是:"+checkVal); 
  } 
} 
</script> 

Combine it with the previous "JQuery method to determine whether a check box is selected" and learn together, you will definitely have unexpected gains!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn