gpt4 book ai didi

java - Window.location.href 有时不工作的原因是什么?

转载 作者:行者123 更新时间:2023-11-28 13:38:35 25 4
gpt4 key购买 nike

jsp 页面,用于 JavaScript 中的提交按钮 onclick 功能。当我第一次单击按钮时,未重定向到 CallService.jsp。我单击该按钮两到三次后,只有该页面会被重定向。

请给我解决方案或原因

我的代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

var uname = document.getElementById("uname").value;
//var pwd = document.getElementById("pwd").value;

window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" >
<input type="reset" value="Cancel">

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


String uname = request.getParameter("username");
//String pwd = request.getParameter("password");


out.println(uname);

%>

最佳答案

您的按钮类型是

<input type="submit" value="Submit" onclick="sample(this);" > 

将其替换为:

<input type="button" value="Submit" onclick="sample(this);" > 

在提交表单时调用您的函数,如下所示:

<form method="post" name="fromname" onsubmit="yourfunction()">

关于java - Window.location.href 有时不工作的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19560239/

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