2013-02-16 129 views
0

這裏是情況。我有一個下拉菜單。通過從數據庫中提取一些值來填充下拉菜單中的選項。要做到這一點下面是我做了什麼..: -更改組合框值時從數據庫動態更新文本框的值

<select name="product_list" onchange="selectProduct(this.value)"> 
    <option value="none">Select one</option> 
     <% 
      List<String> options = new ArrayList<String>(); 
      DynamicCombo comboBox = new DynamicCombo(); 
      options = comboBox.generateComboBox(); 
      Collections.sort(options); 
      int tempVar = 0; 
      while (tempVar < options.size()) { 
       out.print("<option value=\""); 
       out.print(options.get(tempVar)); 
       out.print("\">"); 
       out.print(options.get(tempVar)); 
       out.print("</option>"); 
       tempVar++; 
      } 
     %> 
</select> 

DynamicCombo是,有一個名爲「generateComboBox()方法的類。此方法只返回一個數組列表,其中包含從數據庫中提取的所有值,這是我需要在前端(jsp頁面)的下拉框中顯示的值。在我的jsp頁面上,我只是遍歷這個列表並將其作爲選項進行適當的打印。 這工作絕對好。

現在我在窗體上有另一個文本框,說'textbox1'。現在要求這個文本框的值應該根據用戶從上面的下拉框中選擇的內容來更新。

因此,例如,如果用戶從下拉框中選擇'prod1'(它是後端數據庫表中的主鍵)選項,則應該從數據庫表中獲取相應的值(產品名稱),然後應該在名爲'textbox1'的文本框中更新。

另一件事是整個事情被包含在一個應該最終提交給servlet進行進一步處理的表單中。

那麼我該如何做到這一點。

+0

什麼正在做平變化的功能selectProduct?我認爲你必須更新此功能中的文本框 – 999k 2013-02-16 04:26:07

+0

好吧,我寫了該功能,只是試圖獲得用戶選擇的價值。我不介意更新此功能中的文本框。但是,我如何從後端獲取更新值,這取決於用戶選擇。 – qre0ct 2013-02-16 04:33:29

+0

每當用戶選擇一個selectProduct函數被調用的值,你將得到選擇的值 – 999k 2013-02-16 04:40:47

回答

1

我想出解決我的自己的問題。這可能不是最優雅的做法,但它做得很好。

因此,根據我的要求,我確實想要做的是......將一個值(將從數據庫中提取)插入到表單上的文本框中,具體取決於用戶從下拉列表中選擇的內容框已經存在於我的表單上。

爲了達到這個目的,我想了一想,如果我可以用我的主窗體嵌套窗體,它會解決我的問題。但是我發現不允許嵌套表格。因此,我想到的下一個選擇是在沒有用戶單擊提交按鈕的情況下如何提交相同的表單,並將其作爲「不完整」提交進行適當處理(從表單中仍然需要用戶手動提交)通過點擊提交按鈕)在服務器上。

所以我簡單地使用了下拉框的'onChange'事件。我在表單上創建了一個額外的隱藏字段。我寫了一個簡單的javascript函數,它將隱藏字段的值設置爲字符串「partial Submit」,並將我的主表單(稱爲'form1')提交爲: -

document.getElementById("hidden_id").setAttribute("value","partial submit"); 
form1.submit; 

每當(和每次)下拉框的onchange事件被觸發時,會執行上述功能。

當用戶最終點擊表單上的提交按鈕以提交最終完成的表單時,將調用另一個javascript函數,該函數簡單地將表單上隱藏字段的值設置爲字符串「final submit」,並且將提交形式: -

document.getElementById("hidden_id").setAttribute("value","final submit"); 
form1.submit; 

現在我的服務器上,我檢查這個隱藏字段的值: -

if(request.getParameter("hidden_id").equals("partial Submit")) 
{ 
    // make a database connection, pass the value user selected from the drop down box 
    // to a prepared statement that does the query for getting the 'productName' from 
    // the database, collect the returned string in a variable and set a 
    // request attribute with this returned value. This value can simply be used in the 
    // jsp to fill in the value part of the textbox1. 
} 
else 
{ 
    if(request.getParameter("hidden_id").equals("final Submit")) 
    { 
    // do the rest of the final processing that needs to be done when user finally 
    // submits the completed form. 
    } 
    else 
    { 
    // throw an exception to take care of the possibility that the user might send 
    // in a 3rd value as a value for the hidden field. 
    } 
} 
0

既然你還沒有提供selectProduct(this.value)的代碼,我認爲它提交的jsp頁面,當你改變下拉的值。

如果在的servelt的情況下,設置要在JSP中顯示在請求對象

request.setAttribute("valuetodisplay" ,valuetodisplay); 

的價值,現在在JSP

<input type="text" value ='<%= request.getAttribute("valuetodisplay")%>' />