How to keep the selected value of the drop-down list in Java (struts)
I use three interdependent drop - down lists
The user must select the first drop-down list Therefore, the second list will be populated
The user will then select the second drop-down list again and populate the third list
In the third drop-down list selection, the results are populated For the entire drop-down list, I use ajax But for the last drop-down list, I'm using the action, so I have to submit the form
Once the form is returned after the search results The result is a normal population
However, the last two drop-down lists are not populated with the selected values
Therefore, I will assign values when unloading the page, as shown below
document.getElementById("laneid").text = '<%=laneID%>';
I tried this, too
document.getElementById("laneid").value = '<%=laneID%>';
I'm calling Ajax to populate all the values in the drop - down list The value comes from the request variable '<% = laneid% >'
Please help me preserve my values
I think jQuery can help in this situation However, if there are any other solutions, please advise
Thank you very much
Solution
Romeo,
Cannot access using select
document.getElementById("laneid").text = '<%=laneID%>'; document.getElementById("laneid").value = '<%=laneID%>';
Instead, you should use
var selLaneID = document.getElementById("laneid"); // get the element selLaneID.options[selLaneID.selectedIndex].value; // gives the value of selected option selLaneID.options[selLaneID.selectedIndex].text; // gives the selected text
In jQuery:
$("#laneid").val(); // gives value of selected text $("#laneid option:selected").text(); // gives the selected text $("#laneid").text(); // gives ALL text in SELECT $("#laneid").val("val3"); // selects the option with value="val3"
If you want to set the dropdon value in JavaScript, using the jQuery method will be the easiest
With struts, you can also populate and select the drop-down list with < HTML: Select > And < HTML: optionscollection > tags Take a look