2010-03-31 53 views
0

客户机代码检测下拉列表的值是很简单:问题与服务器(servlet的)侧

<form action="DDServlet" method="post"> 
     <input type="text" name="customerText"> 
     <select id="customer"> 
      <option name="customerOption" value="3"> Tom </option> 
      <option name="customerOption" value="2"> Harry </option> 
     </select> 
     <input type="submit" value="send"> 
</form> 

这里是关于Servlet

Enumeration paramNames = request.getParameterNames(); 
while(paramNames.hasMoreElements()){ 
     String paramName = (String)paramNames.nextElement(); //get the next element 
     System.out.println(paramName); 
} 

当我打印的代码我只看到,customerText,但不是customerOption。任何想法为什么家伙?我希望,如果我在选项中选择Tom,一旦我提出,在我的servlet我应该能够做到这一点:String paramValues[] = request.getParameterValues(paramName);并取回的3

回答

2

你需要把name属性的选择值。这应该解决它:

<select name="customerOption" id="customer"> 
    <option value="3"> Tom </option> 
    <option value="2"> Harry </option> 
</select> 
+0

+1,参数名称将为customerOption,而不是客户。我通常保持我的id和姓名字段相同,但它并不重要。 – 2010-03-31 02:11:49

+0

非常感谢 – 2010-03-31 02:12:36

0

您显示的代码,你没有getParameterNames。这仅仅是个例子还是那个错误?

+0

'getParameterNames()'return'Enumeration',然后你可以通过'hasMoreElement()'遍历表单中的所有元素。确保你的html标签包含'name'属性。 – 2010-04-01 19:25:59