[英]onsubmit is failing to javascript function
在下面的代码中,有一个表单可以为新用户创建登录名。 然后按继续按钮,它应该调用“ validateForm()”函数。 如果要检查表格中填写的值,并且缺少适当的值,则应发出警报消息。 但是,即使出现错误也不会发出任何警报,就像我按“继续”一样,即使表单完全为空。
<html>
<head>
<title></title>
<script type="text/javascript">
function validateForm()
{
var email=document.forms["newlogin"]["email"].value;
var cemail=document.forms["newlogin"]["cemail"].value;
var pass=document.forms["newlogin"]["pwd"].value;
var cpass=document.forms["newlogin"]["cpwd"].value;
var answ1=document.forms["newlogin"]["ans1"].value;
var answ2=document.forms["newlogin"]["ans2].value;
alert("I am IN");
if(email==null || email=="" || cemail==null || cemail=="") {
alert("Enter Email ID and confirm.");
return false;
}
if(pass==null || pass=="" || cpass==null || cemail=="") {
alert("Enter Password and confirm.");
return false;
}
if(answ1==null || answ1=="" || answ2=null || answ2=="") {
alert("Enter Answer 1 & Answer 2.");
return false;
}
if(email!=cemail) {
alert("Confirmed Email ID is not matching.");
return false;
}
if(pass!=cpass) {
alert("Confirmed Password is not matching.");
return false;
}
}
</script>
</head>
<body>
<h1 style="font:swis721 bt">enginenc</h1>
<form name="newlogin" method="POST" action="http://localhost/cgi-bin/createlogin" onsubmit="return validateForm()">
<table align="center" cellpadding="10" cellspacing="0" style="margin-top:2cm;background-color:#FFD700;border-top:1px solid;border-bottom:1px solid;border-left:1px dotted;border-right:1px dotted" border=0>
<tr>
<td align="left" style="font:westwood let">Enter a valid Email ID:</td>
<td align="left" style="font:westwood let"><input type="text" name="email"></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Confirm the Email ID:</td>
<td align="left" style="font:westwood let"><input type="text" name="cemail"></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Enter password:</td>
<td align="left" style="font:westwood let"><input type="password" name="pwd"></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Confirm Passowrd:</td>
<td align="left" style="font:westwood let"><input type="password" name="cpwd"></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Select Question 1:</td>
<td align="left" style="font:westwood let"><select name="ques1"><option value=1>1. What is your mother's maiden name?</option><option value=2>2. Which is your birth city?</option><option value=3>3. Which is your favorite place?</option></select></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Answer 1:</td>
<td align="left" style="font:westwood let"><input type="text" name="ans1"></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Select Question 2:</td>
<td align="left" style="font:westwood let"><select name="ques2"><option value=1>1. Which is your dream car?</option><option value=2>2. What is your nick name?</option><option value=3>3. Who is your favorite singer?</option></select></td>
</tr>
<tr>
<td align="left" style="font:westwood let">Answer 2:</td>
<td align="left" style="font:westwood let"><input type="text" name="ans2"></td>
</tr>
<tr>
<td align="left" style="font:westwood let"><input style="margin-left:3cm;padding:11px" type="submit" value="Continue"></td>
<td align="left" style="font:westwood let"><input style="padding:11px" type="reset"></td>
</tr>
</table>
</form>
</body>
</html>
在这里,使用onsubmit()=“ return validateForm()”函数提交表单时。 窗体未调用函数validateForm() 。 我尝试了很多,但没有找到错误。 请帮忙。 谢谢。
您有错别字:
var answ2=document.forms["newlogin"]["ans2].value;
-缺少“
if(answ1==null || answ1=="" || answ2=null || answ2=="")
-丢失==
然后它应该工作。
更改
var answ2=document.forms["newlogin"]["ans2].value;
至
var answ2=document.forms["newlogin"]["ans2"].value; //missing quote
和
if(answ1==null || answ1=="" || answ2=null || answ2=="") {
至
if(answ1==null || answ1=="" || answ2==null || answ2=="") {
有一些语法错误。
检查此行。 该行中缺少"
。将行从
var answ2=document.forms["newlogin"]["ans2].value;
至,
var answ2=document.forms["newlogin"]["ans2"].value;
并且,您必须使用==
来检查if语句中的条件。 但是在下面一行中,您仅使用了一个=
。 更改为
if(answ1==null || answ1=="" || answ2=null || answ2=="") {
alert("Enter Answer 1 & Answer 2.");
return false;
}
至,
if(answ1==null || answ1=="" || answ2==null || answ2=="") {
alert("Enter Answer 1 & Answer 2.");
return false;
}
建议:使用firebug或javascript错误控制台找出这些错误。
尝试添加到调用函数单词“ this ”中: <form name="newlogin" method="POST" action="http://localhost/cgi-bin/createlogin" onsubmit="return validateForm(this)">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.