onsubmit没有在chrome中调用js函数,而是直接提交表单

时间:2012-06-25 18:32:32

标签: javascript windows google-chrome

我的表单在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;

2 个答案:

答案 0 :(得分:0)

尝试传入event参数并运行event.preventDefault(),以便在验证之前停止提交表单。

答案 1 :(得分:0)

  1. 您没有使用}关闭函数'validateForm'。因此可能会抛出错误。
  2. 使用onsubmit="return validateForm(this);"请注意,属性名称为onsubmit,没有驼峰大小写且参数为this
  3. validateForm应该是一个全局函数。
  4. 工作示例:http://jsfiddle.net/qrZ8a/3/