<form>标签定义中需要指定onsubmit事件触发时的动作,一般是指定一个处理函数。在此程序中,规定事件触发时执行login()函数,如果login()函数的返回值为ture,则执行下一步动作,即进入到action指定的下一个页面“11-20.html",如果login()函数的返回值是false,则保持当前页面。但我执行时不是这样的
<html>
<head>
<script type ="text/javascript">
function login(){
var userNmae = document.getElementById("userName").value;
var pwd = document.getElementById("pwd").value;
var matchResult = false;
if (userName = ="fjct"||pwd = ="123"){
alert("请确认用户名和登录密码输进正确!")
matchResult = true;
}
return matchResult;
}
</script>
<title>form事件的例子</title>
</head><body>
<form name ="loginForm" action ="11-20.html" onsubmit ="return login()" method ="post">
<table bgcolor ="#87ceeb" width ="200" cellspacing ="0" cellpadding ="0" border ="0" align ="left" >
<tr>
<td class ="table-title" colspan ="2" align ="center" bgcolor ="#4682b4">用户登录</td>
</tr>
<tr>
<td width ="50" height="28" align ="right" >用户名</td>
<td><input id ="userName" name ="userName" type ="text" class ="input"></td>
</tr>
<tr>
<td width ="50" height ="28" align ="right" >密 码</td>
<td><input id ="pwd" name ="pwd" type ="password" class ="input"></td>
</tr>
<tr>
<td width ="50' height ="28" align ="right"></td>
<td><input type ="submit" value ="登录"><input type ="button" value="取消" onClick ="reset()"></td>
</tr>
</table>
</form></body>
</html>
<html>
<head>
<script type ="text/javascript">
function login(){
var userNmae = document.getElementById("userName").value;
var pwd = document.getElementById("pwd").value;
var matchResult = false;
if (userName = ="fjct"||pwd = ="123"){
alert("请确认用户名和登录密码输进正确!")
matchResult = true;
}
return matchResult;
}
</script>
<title>form事件的例子</title>
</head><body>
<form name ="loginForm" action ="11-20.html" onsubmit ="return login()" method ="post">
<table bgcolor ="#87ceeb" width ="200" cellspacing ="0" cellpadding ="0" border ="0" align ="left" >
<tr>
<td class ="table-title" colspan ="2" align ="center" bgcolor ="#4682b4">用户登录</td>
</tr>
<tr>
<td width ="50" height="28" align ="right" >用户名</td>
<td><input id ="userName" name ="userName" type ="text" class ="input"></td>
</tr>
<tr>
<td width ="50" height ="28" align ="right" >密 码</td>
<td><input id ="pwd" name ="pwd" type ="password" class ="input"></td>
</tr>
<tr>
<td width ="50' height ="28" align ="right"></td>
<td><input type ="submit" value ="登录"><input type ="button" value="取消" onClick ="reset()"></td>
</tr>
</table>
</form></body>
</html>
alert("请确认用户名和登录密码输进正确!")
matchResult = true;
}你的判断语句只要求有一个符合都可以跳转到另一个页面了。还有你的= =不知道是在这里写错了还是你的源代码写错,应该写成==吧,中间不能有空格。
楼上说的对==没有空格。
下面组件最好初始值给空
<input id ="userName" name ="userName" type ="text" class ="input" value="">
<input id ="pwd" name ="pwd" type ="password" class ="input" value="">
以免在没有填值的情况下出错。
||是或啊,,只是满足条件之一,,应该用且哦
if (userName = ="fjct"||pwd = ="123"){ 直接报错 呵呵
<td><input type ="submit" value ="登录"><input type ="button" value="取消" onClick ="reset()"></td>很明显的拼写错误
难道楼主你没显示脚本错误提示?
alert("请确认用户名和登录密码输进正确!");
难道楼主是纯记事本编写么?
至于为什么总是执行跳转,肯定是"||"用错了
if (userName = ="fjct"||pwd = ="123"){ 这句中:= =应该是== 连接的不应该是||而应该是&&
如果用户名和密码你只想匹配任意一个的话,你就不会改||;如果你是想两个都匹配的话,就把||改为&&
var userName = document.getElementById("userName").value;
var pwd = document.getElementById("pwd").value;
var matchResult = true;
if (userName !="fjct" || pwd !="123"){
alert("请确认用户名和登录密码输进正确!");
matchResult = false;
}
return matchResult;
}
这样写就可以了,你if中的判断逻辑没有弄清楚啊!
<head>
<script type ="text/javascript">
function login(){
var userName = document.getElementById("userName").value;
var pwd = document.getElementById("pwd").value;
var matchResult = true;
if (userName =="fjct"&&pwd =="123"){
alert("请确认用户名和登录密码输进正确!")
matchResult = false;
}
return matchResult;
}
</script>
<title>form 事件的例子</title>
</head><body>
<form name ="loginForm" action ="11-20.html" onsubmit ="return login()" method ="post">
<table bgcolor ="#87ceeb" width ="200" cellspacing ="0" cellpadding ="0" border ="0" align ="left" >
<tr>
<td class ="table-title" colspan ="2" align ="center" bgcolor ="#4682b4">用户登录</td>
</tr>
<tr>
<td width ="50" height="28" align ="right" >用户名</td>
<td><input id ="userName" name ="userName" type ="text" class ="input" value=""></td>
</tr>
<tr>
<td width ="50" height ="28" align ="right" >密 码</td>
<td><input id ="pwd" name ="pwd" type ="password" class ="input" value=""></td>
</tr>
<tr>
<td width ="50" height ="28" align ="right"></td>
<td><input type ="submit" value ="登录"><input type ="button" value="取消" onClick ="reset()"></td>
</tr>
</table>
</form></body>
</html>
我按照你们的意思改了 仍然是错误 即我的执行结果是无论输了什么账户名和密码 都会执行下一步即进了11-20.html
你的主要问题就是if中的逻辑没有弄清楚,到上一个你发的代码还在错误!
if (userName =="fjct"&&pwd =="123")这么写的话你里面的代码是不是应该改一下啊??????
alert("请确认用户名和登录密码输进正确!")
matchResult = false;
}
return matchResult;
}
/*楼主,
以上代码有逻辑错误,
if为真时,return false,
条件不满足时倒是为return true了 */
<head>
<script type="text/javascript">
function login(){
var userName= document.getElementById("userName").value;
var pwd= document.getElementById("pwd").value;
var matchResult= false;
if (userName=="fjct" && pwd=="123"){
matchResult=true;
}else{
alert("请确认用户名和登录密码输进正确!")
}
return matchResult;
}
</script>
<title>form事件的例子</title>
</head><body>
<form name="loginForm" action="11-20.html" onsubmit="return login()" method="post">
<table bgcolor="#87ceeb" width="200" cellspacing="0" cellpadding="0" border="0" align="left" >
<tr>
<td class="table-title" colspan="2" align="center" bgcolor="#4682b4">用户登录</td>
</tr>
<tr>
<td width="50" height="28" align="right" >用户名</td>
<td><input id="userName" name="userName" type="text" class="input"></td>
</tr>
<tr>
<td width="50" height="28" align="right" >密 码</td>
<td><input id="pwd" name="pwd" type="password" class="input"></td>
</tr>
<tr>
<td width="50' height="28" align="right"></td>
<td><input type="submit" value="登录"><input type="button" value="取消" onClick="reset()"></td>
</tr>
</table>
</form></body>
</html>
alert("请确认用户名和登录密码输进正确!")
matchResult = false;
}
return matchResult;
}
看清楚这个逻辑吧如果你输入fjct和123还能跳转吗?