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
Release: 2016-05-16 15:36:23
Original
1843 people have browsed it

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" />葡萄 
Copy after login

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> 
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template