2013-02-16 104 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")%>' />