我有一个复选框列表,我想限制 1 个复选框只能被选中,我发现了一段 javascript 假设可以执行此操作,但无法成功执行它下面是我尝试过的没有运气。
<!DOCTYPE html>
<html>
<head>
<title></title>
<link type="text/css" rel="stylesheet" href="style.css" />
<script type="text/javascript">
function control(j) {
var total=0;
for(var i=0; i < document.form1.ckb.length; i++){
if(document.question.ckb[i].checked){
total =total +1;}
if(total > 1){
alert("Please Select only one")
document.question.ckb[j].checked = false ;
return false;
}
}
} </script>
</head>
<body>
<header>
</header>
<div id='wrapper'>
<section id='description'>
<p>This quiz is compossed by 10 questions, you have to answer at least 7
from 10 to pass the exam.</p>
<h2>Good luck!</h2>
</section>
<div id='questions-number'>
<p>Question <span id='current-question'>1</span> of <span>10</span> </p>
</div>
<section id='questions'>
<p id='question'></p>
<ul>
<li><input type='checkbox' id='checkAnswer0' name = ckb answer0 value=0 onclick=' control(0)'/><label id='answer0'>answer0</label></li>
<li><input type='checkbox' id='checkAnswer1' name = ckb answer0 value=1 onclick=' control(2)'/><label id='answer1'>answer0</label></li>
<li><input type='checkbox' id='checkAnswer2' name = ckb answer0 value=2 onclick=' control(3)'/><label id='answer2'>answer0</label></li>
<li><input type='checkbox' id='checkAnswer3' name = ckb answer0 value=4 onclick=' control(4)'/><label id='answer3'>answer0</label></li>
</ul>
</section>
<div id='next'>
next
</div>
</div>
<script type = "text/javascript" src="js/jquery-1.9.1.js"></script>
<script type = "text/javascript" src= "js/question_script.js"></script>
</body>
</html>