gpt4 book ai didi

Javascript 复选框验证不起作用

转载 作者:行者123 更新时间:2023-11-28 08:30:47 25 4
gpt4 key购买 nike

如果我在 javascript“checkboxlimit(document.forms.world.countries[ ], 0)”上添加国家/地区[ ],则 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>

将其放在方括号 [] 内、引号之间。

关于Javascript 复选框验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com