如何在没有提交按钮的情况下将值从一个jsp页面传递到另一个jsp? [英] How to pass the values from one jsp page to another jsp without submit button?

查看:182
本文介绍了如何在没有提交按钮的情况下将值从一个jsp页面传递到另一个jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的demo1.jsp页面,

This is my demo1.jsp page,

<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

This is my demo2.jsp

<body>
    <%
    String Uname=request.getParameter("usnername");
    String Usecret=request.getParameter("password");
    out.println(Uname);
    out.println(Usecret);
    %>

此代码工作正常,但是,如何在不使用sumbit按钮的情况下从demo1到demo2获取值?即,使用输入type ="button"的<input type="submit" value="go" onclick="window.location.href='demo2.jsp'" /> bt是否可以发送值.

Here this code is working fine but ,How can i get values from demo1 to demo2 without using sumbit button?i.e., <input type="submit" value="go" onclick="window.location.href='demo2.jsp'" /> bt using input type="button" can we send values.

推荐答案

您只需添加以下脚本即可.

You just have to include following script.

<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). 进一步的查询将不胜感激.

So if you got the code, let me tell you, div_id will be id of div tag where you have to show your result. By using this code, you are passing parameters to another page. Whatever the processing is done there will be reflected in div tag whose id is "div_id". You can call showState(this.value) on "onChange" event of any control or "onClick" event of button not submit. Further queries will be appreciated.

这篇关于如何在没有提交按钮的情况下将值从一个jsp页面传递到另一个jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆