Home > Article > Backend Development > js限制checkbox选项个数 php获取多个checkbbox的方法_PHP教程
1,js限制checkbbox勾选个数的代码:
<!DOCTYPE HTML <span PUBLIC</span> "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>限制复选框选择的数量-www.jbxue.com-脚本学堂</title> <script type="text/javascript" > <span function</span> <span checkDate</span><span (n){ </span><span var</span> checkedCount=0<span ; </span><span for</span>(<span var</span> i=0;i<myForm.course.length ;i ++<span ){ </span><span if</span>(myForm.course[i].<span checked){ checkedCount</span>++<span ; } } </span><span if</span>(checkedCount><span n){ alert(</span>"不能选超过三门课程"<span ); </span><span return</span> <span false</span><span ; } } </span></script> </head> <body> <form name="myForm" method="post" id="myForm" action="test.php"><span 请选择这学期的选课(最多三门):</span><br> <input type="checkbox" name="course[]" value="语文" onClick="return checkDate(3)">语文<br> <input type="checkbox" name="course[]" value="数学" onClick="return checkDate(3)">数学<br> <input type="checkbox" name="course[]" value="英语" onClick="return checkDate(3)">英语<br> <input type="checkbox" name="course[]" value="化学" onClick="return checkDate(3)">化学<br> <input type="checkbox" name="course[]" value="物理" onClick="return checkDate(3)">物理<br> <input type="checkbox" name="course[]" value="政治" onClick="return checkDate(3)">政治<br> <input type="submit" value="提交"> <input type="reset" value="取消"> </form> </body> </html>
<?<span php </span><span /*</span><span * * 获取checkbox的值 * edit by www.jbxue.com </span><span */</span> <span $course</span>=<span $_POST</span>['course'<span ]; </span><span for</span>(<span $i</span>=0;<span $i</span><<span count</span>(<span $course</span>);<span $i</span>++<span ) { </span><span if</span>(<span $course</span>[<span $i</span>]!=""<span ) { </span><span echo</span> <span $course</span>[<span $i</span>]."<br/>"<span ; } } </span>?>