日期:2014-05-16  浏览次数:20376 次

在检验表单输入时,为什么弹不出错误信息
<?xml   version= "1.0 "   encoding= "gb2312 "?>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   XHTML   1.1//EN "
"http://www.w3.org/TR/xhtml11/DTD//xhtml11.dtd ">
<html   xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> Illustrate   password   checking </title>
<script   language= "javascript "   type= "text/javascript "   >
<!--
function   chkPasswords(){
    var   init=document.ElementById( "initial ");
    var   sec=document.ElementById( "second ");
    if(init.value== " "){
        alert( "You   did   not   enter   a   password\n "+ "Please   enter   one   now ");
init.focus();
return   false;
    }
    if(init.value!=sec.value){
        alert( "The   two   passwords   you   entered   are   not   the   same\n "+ "Please   re-enter   both   now ");
init.focus();
init.select();
return   false;
    }
    else   return   true;
}
//-->
</script>
</head>

<body>
<h3> Password   Input </h3>
<form   id= "myForm "   action= " ">
<p>
Your   password
<input   type= "password "   id= "initial "   size= "10 "/>
<br> <br>
Verify   password
<input   type= "password "   id= "second "   size= "10 "/>
<br> <br>
<input   type= "reset "   name= "reset "/>
<input   type= "submit "   name= "submit "/>
</p>
</form>
<script   type= "text/javascript ">
<!--
document.getElementById( "second ").onblur=chkPasswords;
document.getElementById( "myForm ").onsubmit=chkPasswords;
//-->
</script>
</body>
</html>

------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN "
"http://www.w3.org/TR/xhtml11/DTD//xhtml11.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> Illustrate password checking </title>
<script language= "javascript " type= "text/javascript " >
<!--
function chkPasswords(){
var init=document.getElementById( "initial ");
var sec=document.getElementById( "second ");
if(init.value== " "){
alert( "You did not enter a password\n "+ "Please enter one now ");
init.focus();
return false;
}
if(init.value!=sec.value){
alert( "The two passwords you entered are not the same\n "+ "Please re-enter both now ");
init.focus();
init.select();
return false;
}
else return true;
}
//-->
</script>
</head>

<body>
<h3> Password Input </h3>
<form id= "myForm " action= " ">