gpt4 book ai didi

javascript - 为什么 javascript 自动提交在我的 jsp 页面中不起作用?

转载 作者:行者123 更新时间:2023-11-28 19:40:06 25 4
gpt4 key购买 nike

我有一个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

按照这个顺序,为什么?如何自动提交?

最佳答案

您的输入按钮的名称是“提交”,因此当您调用 form.submit() 时,它会与您的提交按钮混淆。更改以下行中提交按钮的名称

 <div id="submit_button"><input name="submit" value="Login" id="button_style" type="submit"/></div>

然后以下应该可以工作

document.forms["loginForm"].submit();

您可以在此处找到代码的工作示例 http://jsfiddle.net/Lg7hkepo/

关于javascript - 为什么 javascript 自动提交在我的 jsp 页面中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com