这是我的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'" />
使用输入type =“ button”的bt我们可以发送值吗?
您只需要包括以下脚本。
<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,您必须在其中显示结果。通过使用此代码,您正在将参数传递到另一个页面。无论执行什么处理,都将反映在id为“ div_id”的div标签中。您可以在任何控件的“ onChange”事件或未提交按钮的“ onClick”事件上调用showState(this.value)。进一步的查询将不胜感激。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句