2015-06-26 66 views
0

多年以后,我不得不将脚趾放回EE和javascript世界。我在这里做错了什么......一个简单的表单被提交,在提交按钮的onsubmit事件期间有一个JS块被验证。当我提交表单时,alert("Here")消息框并未出现,并且表单成功提交,即HelloForm显示为空白值,因此我导致相信validate()未被调用。我已经在Eclipse内部Web浏览器以及Chrome中尝试了这一点,并且两次都看到了相同的内容。我假设我不需要在任一浏览器中明确地激活JavaScript,因为它默认情况下会启用。Javascript验证功能未执行

<html> 
<head> 
<script> 
function validate() 
{ 
    alert("Here"); 
    var x = document.forms["inputForm"].["first_name"].value; 
    if (x == null || x == "") { 
     alert("First name must be filled out"); 
     return false; 
    } 
    return true; 

} 
</script> 
</head> 
<body> 

<form name="inputForm" action="HelloForm" method="GET" onsubmit="return validate()"> 
First Name: <input type="text" name="first_name"> 
<br /> 
Last Name: <input type="text" name="last_name" /> 
<input type="submit" value="Submit The Form" /> 
</form> 
</body> 

</html> 
+0

尝试调用此的validate();按钮上的功能点击 – sen32653

回答

1

在这条线将抛出一个语法错误:

document.forms["inputForm"].["first_name"].value 
          ^^^ 

的说法是错误的,应该没有点大括号:

document.forms["inputForm"]["first_name"].value 
//or 
document.forms.inputForm.first_name.value 

Working with Objects

+0

谢谢你。当超时到期时会接受你的回答。 – swingMan

0

你可以使用Html5验证。

<form name="inputForm" action="HelloForm" method="GET" > 
    First Name: <input type="text" name="first_name" required /> 
<br /> 
Last Name: <input type="text" name="last_name" required /> 
<input type="submit" value="Submit The Form" /> 
</form> 

Working Demo