Selected value for JSP drop down using JSTL

后端 未结 7 686
無奈伤痛
無奈伤痛 2020-11-29 04:37

I have SortedMap in Servlet to populate drop down values in JSP and I have the following code

    SortedMap dept = findDepartment();
           


        
相关标签:
7条回答
  • 2020-11-29 05:27

    If you don't mind using jQuery you can use the code bellow:

        <script>
         $(document).ready(function(){
               $("#department").val("${requestScope.selectedDepartment}").attr('selected', 'selected');
         });
         </script>
    
    
        <select id="department" name="department">
          <c:forEach var="item" items="${dept}">
            <option value="${item.key}">${item.value}</option>
          </c:forEach>
        </select>
    

    In the your Servlet add the following:

            request.setAttribute("selectedDepartment", YOUR_SELECTED_DEPARTMENT );
    
    0 讨论(0)
提交回复
热议问题