我编写了一段简单的 javascript 来验证复选框组。javascript 工作正常,但在 html 中正确实现它被证明是困难的。我的尝试要么导致表单被发送,尽管验证失败,或者没有提交按钮,但其他一切正常。有人能帮忙吗?谢谢
<form id="form1" name="form1" method="get" action="pre_process.php">
<input name="q" type="text" value="<?php echo $_GET['q']; ?>" size="67"/>
<input type="submit" id="search_button" onclick= "chkChecks()" />
/////this line of code submits the form even when the javascript returns false
<input class="submit" type="button" onclick="chkChecks()" />
///this code works, doesn't send the form, but also takes away my nice shiny submit button
///////js code/////
function chkChecks(){
isChecked=false
for(var i=0;i<document.forms["form1"]["SearchEngines[]"].length;i++){
if(document.forms["form1"]["SearchEngines[]"][i].checked){
isChecked=true
}
}
if(isChecked){
document.forms["form1"].submit()
}
else{
alert('Please select at least one Search Engine')
return false;
}
}
</script>
////也是被验证的表单部分
<table width="200">
<tr>
<td><label>
<input type="checkbox" name="SearchEngines[]" value="Bing" id="SearchEngines_0" checked="checked" />
Bing</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="SearchEngines[]" value="Blekko" id="SearchEngines_1" checked="checked" />
Blekko</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="SearchEngines[]" value="Entireweb" id="SearchEngines_2" checked="checked" />
Entireweb</label></td>
</tr>