我很困惑,尽管我的formvalidation函数返回false,为什么我的表单仍然提交。以下是我的代码,我几乎无法弄清楚我做错了什么。请帮助大家
这是我的表单脚本
<form name="regForm" action= "/upload.php" method="post" enctype= "multipart/form-data" onsubmit="return validateForm();">
<p><span class="error" id="reportErr"></span></p>
<label>Do you have an Agent?</label> <input type="radio" name="agent" value="YES"><label>YES</label><input type="radio" name="agent" value="NO"><label>NO</label
<label>Are you under a contract</label><input type="radio" name="contract" value="YES"><label>YES</label><input type="radio" name="contract" value="NO"><label>NO</label>
<input type="submit" name="submit" value="Preview" <input type="reset" value="Clear">
javascript函数如下:
function validateForm(){
var formvalid= true;//even when i set to be false it still submits form
//check for agent user selection
var agent = document.forms["regForm"]["agent"].value;
if (agent == "") {
document.getElementbyId("reportErr").innerHTML = "Select if you have an agent or not";
formvalid=false;
}else{
if (agent =="YES"){
//handle user input for agentname
}
}
}
//check for contract user selection
var contract = document.forms["regForm"]["contract"].value;
if (contract==""){
document.getElementbyId("reportErr").innerHTML = "Select if under contract or not";
formvalid = false;
}else{
if (contract =="YES"){
//handle user input for contractname
}
}
if (formvalid==false){
return false;
}
}
答案 0 :(得分:0)