我写了一段简单的javascript来验证复选框组。 JavaScript工作正常,但在html中正确实现它证明是困难的。无论如何,尽管验证失败,我的尝试或者导致表单被发送,或者没有提交按钮,但其他一切正常工作。有人能够帮助吗?感谢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>
u能显示你的js代码的验证类? – Shreedhar 2012-07-21 01:47:05
感谢所有的输入人员,但实际上最后我需要做的就是在这行代码中添加一个_value =“Submit”_ __ input class =“submit”type =“button”onclick =“chkChecks( )“/> _并且一切正常。对不起,如果我没有明确问题。无论如何感谢 – shanahobo86 2012-07-21 09:28:39