2010-03-31 49 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