我的表格在Firefox和IE中工作正常。调用onsubmit时,它将调用js函数并验证代码,如果验证正确,则将提交表单。
但是,在chrome中,它不进行验证,而只是直接提交表单。
我不确定为什么会这样,进行了大量搜索但徒劳无功。
任何帮助将对解决这个问题有很大帮助。
表格代码:
<form name="myform" action="queryfeedback.php" method="post" onSubmit="return validateForm(myform);">
<label for="labelField_Name" id="idLabel_Name"></label>
<input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here"/>
<br />
<label for="labelField_EMail" id="idLabel_EMail"></label>
<input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" />
<br />
<label for="labelField_Message" id="idLabel_Message"></label>
<textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here"></textarea>
<br />
<input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit" alt="Submit Button"/>
</form>
验证码:
function validateForm(form)
{
alert(form.nameField_Name.value);
//alert(formValueEMail.value);
//alert(formValueMessage.value);
if(form.nameField_Name.value=='')
{
alert("Name field is required. Please fill it in.");
form.nameField_Name.focus();
form.nameField_Name.select();
return false;
}
if(form.nameField_Name.value=='Enter your name here')
{
alert("Name field is required. Please fill it in.");
form.nameField_Name.focus();
form.nameField_Name.select();
return false;
}
if(form.nameField_EMail.value=='')
{
alert("E-Mail Address field is required. Please fill it in.");
form.nameField_EMail.focus();
form.nameField_EMail.select();
return false;
}
if(form.nameField_EMail.value=='Enter your E-Mail address here')
{
alert("E-Mail Address field is required. Please fill it in.");
form.nameField_EMail.focus();
form.nameField_EMail.select();
return false;
}
//Checking for correct format of EMail address.
var x=document.forms["myform"]["nameField_EMail"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
if(form.nameField_Message.value=='')
{
alert("Message field is required. Please fill it in.");
form.nameField_Message.focus();
form.nameField_Message.select();
return false;
}
if(form.nameField_Message.value=='Enter your message for us here')
{
alert("Message field is required. Please fill it in.");
form.nameField_Message.focus();
form.nameField_Message.select();
return false;
}
return true;
最佳答案
尝试传递事件参数并运行event.preventDefault()
,以便在验证通过之前停止提交表单。
关于javascript - onsubmit不会在Chrome中调用js函数,而是直接提交表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11195173/