[英]Javascript checkbox validation not working
如果我在我的JavaScript“ checkboxlimit(document.forms.world.countries [],0)”上添加country [],則javascript無法正常工作。 如果我刪除[],則復選框驗證絕對正常。 我該如何解決? 這是我的完整代碼:
第1頁:
<script type="text/javascript">
function checkboxlimit(checkgroup, limit){
var checkgroup=checkgroup
var limit=limit
for (var i=0; i<checkgroup.length; i++){
checkgroup[i].onclick=function(){
var checkedcount=0
for (var i=0; i<checkgroup.length; i++)
checkedcount+=(checkgroup[i].checked)? 1 : 0
if (checkedcount>limit){
alert("You can only select a maximum of "+limit+" checkboxes")
this.checked=false
}
}
}
}
</script>
<form id="world" name="world" method="post" action="example2.php">
<select name="dropdown" onclick="Calc(world)">
<option value=""></option>
<option value="school">School</option>
<option value="college">College</option>
</select>
<p>Countries:</p>
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
function Calc(world){
var entersel1 = document.world.dropdown.value;
if (entersel1 == ''){
checkboxlimit(document.forms.world.countries[], 0)
}
else if(entersel1 == 'school'){
checkboxlimit(document.forms.world.countries[], 2)
}
else{
checkboxlimit(document.forms.world.countries[], 3)
}
}
</script>
第2頁:
<?php
$dropdown = $_POST['dropdown'];
$countries = implode(',', $_POST['countries']);
echo $dropdown.'<br>'.$countries;
?>
<script type="text/javascript">
function Calc(world){
var entersel1 = document.world.dropdown.value;
if (entersel1 == ''){
checkboxlimit(document.forms.world['countries[]'], 0)
}
else if(entersel1 == 'school'){
checkboxlimit(document.forms.world['countries[]'], 2)
}
else{
checkboxlimit(document.forms.world['countries[]'], 3)
}
}
</script>
將其放在引號之間的方括號[]中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.