如果我在我的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>


将其放在引号之间的方括号[]中。

07-24 18:42