Home >Web Front-end >JS Tutorial >JavaScript method to detect and limit the number of checkboxes selected

JavaScript method to detect and limit the number of checkboxes selected

PHPz
PHPzOriginal
2016-05-16 15:45:431266browse

This article mainly introduces the method of using JavaScript to detect and limit the number of selected check boxes. It involves techniques related to the judgment and operation of check boxes in JavaScript. It is very simple and practical. Friends who need it can refer to it. The details are as follows:

Here, JavaScript is used to determine whether the check box is selected, and at the same time, it can limit the number of selected check boxes. This is a more practical JS form judgment example. Whether the check box is selected has puzzled many people. Well now, this code helps you solve the problem.

The screenshot of the running effect is as follows:

The specific code is as follows:

<html>
<head>
<title>判断复选框中否选中</title>
<script>
var check_num = 0;
function check(){ 
 if(event.srcElement.checked==true)
  check_num++;
 else 
  check_num--;   
 if(check_num>3)
 {
  alert("最多只能选3个!");
  event.srcElement.checked=false;
  check_num--;
 }  
}
</script>
</head>
<body>
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<input type="checkbox" name="test" onClick="check()">
<br>
你只能选择3个复选框。
</body>
</html>

The above is the entire content of this chapter, more related tutorials Please visit JavaScript Video Tutorial!

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