[英]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.