我有一个Java Web应用程序,它有一个jsp登录页面,我添加了一个javascript,使其在3秒后自动登录,但它不能正常工作,代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="styles/loginForm.css" type="text/css"/>
<link rel="stylesheet" href="styles/body.css" type="text/css"/>
<script type="text/javascript" src="javascript/login.js"></script>
<c:choose>
<c:when test="${browserType.msie}">
<link rel="stylesheet" href="styles/header_ms.css" type="text/css"/>
</c:when>
<c:otherwise>
<link rel="stylesheet" href="styles/header.css" type="text/css"/>
</c:otherwise>
</c:choose>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Support Logon</title>
<script language="javascript" type="text/javascript">
function initialize()
{
alert("Hello = 1");
setTimeout('submitForm()',3000); //delay 3 seconds
alert("Hello = 10");
}
function submitForm()
{
alert("Hello = 2");
// window.document.loginForm.submit();
document.forms["login_form"].submit();
alert("Hello = 3");
}
</script>
</head>
<body onload="initialize()">
<%@ include file="header.jsp" %>
<div id="login_body">
<form method="post" action="login.html" name="loginForm">
<div id="login_wrapper">
<div id="login_form">
<div id="login_title">Please Login</div>
<div id="fail_message">${failedLoginMessage}</div>
<div id="user_name">
<div id="name_label">Username:</div>
<div id="name_text"><input name="j_username" size="22" id="textbox" value="${commandBean.j_username}"/></div>
</div>
<div id="password">
<div id="pw_label">Password:</div>
<div id="pw_text"><input type="password" name="j_password" size="22" id="textbox" value="${commandBean.j_password}"/></div>
</div>
<div id="remember_me">
<c:choose>
<c:when test="${commandBean.fail}">
<div id="remember_box"><input name="rememberMe" id="checkbox_style" type="checkbox"/></div>
<div id="remember_label">Remember Me</div>
</c:when>
<c:otherwise>
<div id="remember_box"><input checked="checked" name="rememberMe" id="checkbox_style" type="checkbox"/></div>
<div id="remember_label">Remember Me</div>
</c:otherwise>
</c:choose>
</div>
<div id="submit">
<div id="submit_button"><input name="submit" value="Login" id="button_style" type="submit"/></div>
<div id="clear_button"> <input name="clear" value="Clear" id="button_style" type="button" onclick="clearLoginForm();"/></div>
</div>
我尝试了以下所有方法,但都没有奏效:
document.forms["loginForm"].submit();
document.forms["login_form"].submit();
window.document.loginForm.submit();
window.document.login_form.submit();
我得到的只是:
Hello = 1
Hello = 10
[ delay 3 sec. ]
Hello = 2
按顺序,为什么?如何自动提交?
答案 0 :(得分:2)
输入按钮的名称是提交,因此当您调用form.submit()时,它会与您的提交按钮混淆。 在以下行中更改提交按钮的名称
<div id="submit_button"><input name="submit" value="Login" id="button_style" type="submit"/></div>
以下应该有效
document.forms["loginForm"].submit();
找到代码的工作示例
答案 1 :(得分:1)
您是否看到任何脚本错误?
最简单的修复方法,
的document.getElementById(&#34; formid&#34)。提交(); //使用您的表单ID;