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

求助:表单onsubmit返回false为什么还是会跳转?
本帖最后由 u014350328 于 2014-04-15 16:06:55 编辑
程序中使用image对象提交submit方法,相关代码为<form name="form1" action="index.jsp" method="get" onsubmit="return check()" >
在javascript 中 check返回false值依旧会跳转至index.jsp页面,请问如何使数据不符合要求时候不跳转至index.jsp?

完成程序代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page pageEncoding="GBK" %>
<html>

<%
request.setCharacterEncoding("gbk");
String username = request.getParameter("username");
String password = request.getParameter("password");
%>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Box HTML Code - www.PSDGraphics.com</title>
<link href="login-box.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE2 {font-size: 36px}
-->
</style>


<script type="text/javascript">
function check(){
var username = document.form1.username.value;
var password = document.form1.password.value;
if(username == null || username == ""){
alert("用户名不能为空!");
return false;
}else if(password == null || password == ""){
alert("密码不能为空!");
return false;
}else if(username == "admin" && password == "admin"){
alert("redirect");
response.sendRedirect("index.jsp");
return true;
}else{
alert("username=|" + username "|password=|" + password + "|"; 
return false;
}

}
</script>

</head>
<body>
<form name="form1" action="index.jsp" method="get" onsubmit="return check()" >
<div style="padding: 100px 0 0 250px;">
  <div id="login-box">
    <H2>&#29992;&#25143;&#30331;&#38470;<br />
    </H2>
    <div id="login-box-name" style="margin-top:20px;">&#29992;&#25143;&#21517;:</div>
    <div id="login-box-field" style="margin-top:20px;">
      <input name="username" class="form-login" title="Username" value="" size="30" maxlength="2048" />
    </div>
    <div id="login-box-name">&#23494;&#30721;:</div>
    <div id="login-box-field">
      <input name="password" type="password" class="form-login" title="Password" value="" size="30" maxlength="2048" />
    </div>
    <br />
    <span class="login-box-options">
    <input type="checkbox" name="1" value="1">
    Remember Me <a href="#" style="margin-left:30px;">Forgot password?</a></span> <br />
 &n