2013-08-06 19 views
0

我有一個JQuery的下拉菜單,像這樣jQuery的下拉菜單作爲<form><select>

​​

類似this

我所要做的是使用每個li作爲formselect每個poption

例如:將鼠標懸停在項目,然後選擇所需的項目,提交表單和servlet現在重定向到同一頁面填充表選項,然後將鼠標懸停表並選擇所需的表等...

我唯一的問題是,在我使用form select之前,從下拉列表value中選擇的選項將以select標記的名稱作爲輸入。然後我將在Servlet中引用request.getParameter("name_of_input");在這種情況下,我該如何做到這一點?

回答

0

我想出一個辦法,使這項工作

<%! int variableName = -1 ; /* This is the **value** for your input */ %> 

<div id="dropdownmenu"> 
    <form name="dropdownlist" action="<!--TO YOUR SERVLET-->" method="POST" id="dropdown"> 

    <ul> 
     <li> 
      <p><a href="#">Project</a></p> 
      <%for(CProject project: projectList) 
       { %> 
        <p><a href="#" onclick="<%variablename = project.getName(); /* This will place the value of the chosen option in the variableName */%> submitform();"> 
        <%= project.getName() %></a></p> 

       <% 
       } %> 

       <input type="hidden" name="projectselect" value='<%= variableName %>'/> 
     </li> 
    </ul> 

</form> 

我只展示瞭如何在第一li有多個輸入發送所有你需要做的就是添加另一個做到這一點,但是變量位於<%! %>之間,將隱藏的input置於li標記的底部,並使其值等於您選擇的變量名稱。 詳見代碼澄清

0

我最後的答案沒有正確地工作,這裏是修復

<div id="dropdownmenu"> 
    <form name="dropdownlist" action="<!--Servlet-->" method="POST" id="dropdown">  

     <ul> 
      <li> 
       <p><a href="#">Project</a></p> 
       <% int counter = 0; 
       for(CProject project: projectList) 
       { %>                 <!-- \/ This script submits the form \/--> 
        <p><a href="#" onclick="document.getElementById('<%=project.getName()%>').click(); submitform()";> 
        <input id='<%=project.getName()%>' onclick ="this.name = 'nameWanted'"<!--This acts as the name of the <select> tag--> type="hidden" value ="<%= counter %>" <!--This value is different for each option in the for loop --> /> 
        <%= project.getName()%></a></p> 
       <% 
        counter++; 
       } %> 

       </li> 
      </ul> 

     </form> 
    </div> 

這裏這樣會爲每個新元素的隱含input。每個輸入只有在單擊元素時纔有名稱,並且通過for循環設置值。