Good day! There was a problem with the implementation of such that if you select a value in one selecte, then the values ​​change in another. For example, there is a list of departments of the hospital and when choosing a particular department in select, the doctors of this particular department are displayed. The bottom line is that if you select a value in select departments, and then send it to a servlet to change the value in select doctors, the list of specific doctors changes, and the value of the department selection returns to the initial one. Question: how to implement it so that after sending the form to the servlet and displaying the select with specific doctors, the value of the first select does not change?

Here is my code:

<form class="form-horizontal" action="controller" method="POST"> <input type="hidden" name="command" value="insertNewReception"/> Отделение: <select class="selectpicker"> <option onclick="location.href='/Task/controller?command=listCategory&category=neurologCategory&reception=new';"> <c:if test="${category=='Невропатологическое'}"> <c:out value="${category}"></c:out> </c:if> </option> <option onclick="location.href='/Task/controller?command=listCategory&category=oftalmologCategory&reception=new';"> <c:if test="${category=='Офтальмологическое'}"> <c:out value="${category}"></c:out> </c:if> </option> <option onclick="location.href='/Task/controller?command=listCategory&category=pediatrCategory&reception=new';"> <c:if test="${category=='Детское'}"> <c:out value="${category}"></c:out> </c:if> </option> <option onclick="location.href='/Task/controller?command=listCategory&category=terapevtCategory&reception=new';"> <c:if test="${category=='Терапевтическое'}"> <c:out value="${category}"></c:out> </c:if> </option> </select> Врач: <select class="selectpicker"> <c:forEach var="doctor" items="${listCategoryDoctors}"> <option>${doctor.lastName}</option> </c:forEach> </select> <button type="submit" class="btn btn-primary">Записать</button> <br/> </form> 

I will be glad to any answer!

    1 answer 1

    First, it is not necessary to fix the changes in the first select in onclick with option elements. This is the wrong approach. For example, I often select values ​​in the drop-down lists using the keyboard and your onclick will not work.

    It is necessary to use the intended event change on the select element for this purpose: http://htmlbook.ru/html/attr/onchange

    Secondly, I would recommend using an ajax request to get the values ​​of the second select , which will retrieve data and redraw the select without refreshing the page.

    But if you don’t want to change a lot in the logic of the work, you just need to transfer the current value selected in the first select to the second page, where the appropriate option is the selected attribute.

    In your case, the code will be something like this:

     <form class="form-horizontal" action="controller" method="POST"> <input type="hidden" name="command" value="insertNewReception"/> Отделение: <select class="selectpicker" onchange="location='/Task/controller?command=listCategory&category='+this.value+'&reception=new'"> <option value="neurologCategory" ${param.category == "neurologCategory" ? 'selected="selected"' : ''}> <c:if test="${category=='Невропатологическое'}"> <c:out value="${category}"></c:out> </c:if> </option> <option value="oftalmologCategory" ${param.category == "oftalmologCategory" ? 'selected="selected"' : ''}> <c:if test="${category=='Офтальмологическое'}"> <c:out value="${category}"></c:out> </c:if> </option> <option value="pediatrCategory" ${param.category == "pediatrCategory" ? 'selected="selected"' : ''}> <c:if test="${category=='Детское'}"> <c:out value="${category}"></c:out> </c:if> </option> <option value="terapevtCategory" ${param.category == "terapevtCategory" ? 'selected="selected"' : ''}> <c:if test="${category=='Терапевтическое'}"> <c:out value="${category}"></c:out> </c:if> </option> </select> Врач: <select class="selectpicker"> <c:forEach var="doctor" items="${listCategoryDoctors}"> <option>${doctor.lastName}</option> </c:forEach> </select> <button type="submit" class="btn btn-primary">Записать</button> <br/> </form> 

    What about the ${param.category == "terapevtCategory" ? 'selected="selected"' : ''} ${param.category == "terapevtCategory" ? 'selected="selected"' : ''} not sure, unfamiliar with the syntax, but I hope to correct it if there is an error.