2014-04-25 89 views
2

这是我的demo1.jsp页,如何在不提交按钮的情况下将值从一个jsp页面传递给另一个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); 
    %> 

下面这段代码工作正常,但我怎样才能得到从demo1到demo2的值不使用sumbit按钮?即,<input type="submit" value="go" onclick="window.location.href='demo2.jsp'" /> bt使用输入类型=“按钮”我们可以发送值。

+0

问题是不理解可以更多地讨论 “不透过按钮”? –

+0

在哪个事件上你想提交表单? –

+0

那么你如何导航到demo2.jsp? – Wanderer

回答

4

你只需要包括以下脚本。

<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标签中。 你可以调用showState(this。值)在任何控件的“onChange”事件或按钮未提交的“onClick”事件。 进一步的查询将不胜感激。

0

你能做到这一点在任何的这种方法,引发在这样一个表单按钮的onclick

<form id="myform" name="myform" method="post" action="demo2.jsp"> 
    <input type="text" name="usnername" /> 
    <input type="text" name="password"/>   
    <input type="button" value="go" onclick="submitForm" /> 
</form> 

而且使用javascript

 function submitForm() {     
      document.forms[0].submit(); 
      return true; 
     } 

,或者你也可以尝试Ajax发布您的页面

这里是链接jQueryAjax

而且还有不错的启动示例using Ajaxhere

希望这有助于!

2

我想了解您的问题,并且您似乎希望第一个JSP中的值在第二个JSP中可用。

  1. 这是非常糟糕的习惯放置Java代码片段在JSP文件中,以便代码片段应该去一个servlet。

  2. 选择一个servlet中的值即。在应用程序只要会话有效

    HttpSession sess = request.getSession(); 
    sess.setAttribute("username", username); 
    sess.setAttribute("password", password); 
    
  3. 这些值将可在任何地方:

    String username = request.getParameter("username"); 
    String password = request.getParameter("password"); 
    
  4. 然后存储值的会话中。

    HttpSession sess = request.getSession(false); //use false to use the existing session 
    sess.getAttribute("username");//this will return username anytime in the session 
    sess.getAttribute("password");//this will return password Any time in the session 
    

我希望这是你想知道,但请不要在JSP中使用的代码片段的东西。您可以在JSP中使用jstl总是得到的值到JSP:

${username}//this will give you the username in the JSP 
${password}// this will give you the password in the JSP 
0

我不完全知道你想做什么。但是你无法使用另一种形式的提交按钮发送一个表单的数据。

您可以使用会话或使用具有提交按钮的隐藏字段来做一件事。您可以使用javascript/jquery将值从第一个表单传递到第二个表单的隐藏字段。然后您可以提交表单。

或者最简单的做法是使用会话。

<form> 
<input type="text" class="input-text " value="" size="32" name="user_data[firstname]" id="elm_6"> 
<input type="text" class="input-text " value="" size="32" name="user_data[lastname]" id="elm_7"> 
</form> 

<form action="#"> 
<input type="text" class="input-text " value="" size="32" name="user_data[b_firstname]" id="elm_14"> 
<input type="text" class="input-text " value="" size="32" name="user_data[s_firstname]" id="elm_16"> 

<input type="submit" value="Continue" name="dispatch[checkout.update_steps]"> 
</form> 


$('input[type=submit]').click(function(){ 
$('#elm_14').val($('#elm_6').val()); 
$('#elm_16').val($('#elm_7').val()); 
}); 

这是它http://jsfiddle.net/FPsdy/102/

2

注意的jsfiddle:请给出准确Form1.jsp路径细节test.jsp的 1 test.jsp的和2.Form1.jsp

测试。 JSP

<body> 
<form method ="get" onsubmit="Form1.jsp"> 
<input type="text" name="uname"> 
<input type="submit" value="go" ><br/> 
</form> 
</body> 

Form1.jsp

</head> 
<body> 
<% String name=request.getParameter("uname"); out.print("welcome "+name); %> 
</body> 
0

你可以试试这个办法还,

HTML:

<form action="javascript:next()" method="post"> 
<input type="submit" value=Submit /></form> 

的Javascript:

 function next(){ 
     //Location where you want to forward your values 
     window.location.href = "http://localhost:8563/And/try1.jsp?dymanicValue=" + values; 
     } 
相关问题