Home > Web Front-end > JS Tutorial > A simple JavaScript method to determine whether a check box is selected and get the value_javascript skills

A simple JavaScript method to determine whether a check box is selected and get the value_javascript skills

WBOY
Release: 2016-05-16 15:45:17
Original
1272 people have browsed it

The example in this article describes how to use JavaScript to simply determine whether a check box is selected and extract the value. Share it with everyone for your reference. The details are as follows:

JavaScript is used here to get the value of the check box. When friends first start using forms to submit data, they usually encounter a special element in the form - the check box, to determine whether it is selected and to get its value. The value seems to be a little different from other form elements. This code will show you how to get the value of the check box and determine whether it is selected.

The operation effect is as shown below:

The specific code is as follows:

<title>JavaScript 获取复选框的值</title>
<script>
function checkbox()
{
var str=document.getElementsByName("box");
var objarray=str.length;
var chestr="";
for (i=0;i<objarray;i++)
{
 if(str[i].checked == true)
 {
  chestr+=str[i].value+",";
 }
}
if(chestr == "")
{
 alert("请先选择复选框~!");
}
else
{
 alert("复选框的值是:"+chestr);
}
}
</script>
选择您的拿手技术:
<input type="checkbox" name="box" id="box1" value="ASP" />ASP
<input type="checkbox" name="box" id="box2" value="PHP" />PHP
<input type="checkbox" name="box" id="box3" value="JSP" />JSP
<input type="button" name="button" id="button" onclick="checkbox()" value="提交" />
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
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