这是我的demo1.jsp页面,
<body>
<form id="myform" name="myform" method="post" action="demo2.jsp">-->
<input type="text" name="usnername" />
<input type="text" name="password"/>
<input type="submit" value="go" onclick="window.location.href='demo2.jsp'" />
</form>
这是我的demo2.jsp
<body>
<%
String Uname=request.getParameter("usnername");
String Usecret=request.getParameter("password");
out.println(Uname);
out.println(Usecret);
%>
这里的代码工作正常,但是,如何在不使用 sumbit 按钮的情况下从 demo1 获取值到 demo2?即 <input type="submit" value="go" onclick="window.location.href='demo2.jsp'" />
bt 使用 input type="button"我们可以发送值吗。
最佳答案
您只需包含以下脚本。
<script language="javascript" type="text/javascript">
var xmlHttp
function showState(str)
{
//if you want any text box value you can get it like below line.
//just make sure you have specified its "id" attribute
var name=document.getElementById("id_attr").value;
if (typeof XMLHttpRequest != "undefined")
{
xmlHttp= new XMLHttpRequest();
}
var url="forwardPage.jsp";
url +="?count1=" +str+"&count2="+name";
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("div_id").innerHTML=xmlHttp.responseText
}
}
</script>
所以如果你得到了代码,让我告诉你,div_id 将是你必须显示结果的 div 标签的 ID。 通过使用此代码,您将参数传递到另一个页面。无论进行何种处理,都会反射(reflect)在 id 为“div_id”的 div 标签中。 您可以在任何控件的“onChange”事件或未提交按钮的“onClick”事件上调用 showState(this.value)。 进一步查询将不胜感激。
关于jsp - 如何在没有提交按钮的情况下将值从一个jsp页面传递到另一个jsp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23286619/