2014-04-25 45 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; 
     } 
相關問題