How to save selected dropdown value in java (struts)

I use three drop-down lists that are interdependent.

The user must select the first drop-down list. Accordingly, the second list will be populated.

Then again the user will select the second drop-down list, then the third list will be filled.

In the third drop-down list, select the result. For the whole dropdown, I use Ajax. But for the last dropdown, I use action , so I need to submit the form.

As soon as the form returns after the search result. Results - this is the population properly.

But the last two drop-down lists are not populated with the selected value.

So, for this, I assign a value while loading the page below

 document.getElementById("laneid").text = '<%=laneID%>'; 

I tried this too

 document.getElementById("laneid").value = '<%=laneID%>'; 

To return from the action, I call ajax to populate all the dropdown values. the values ​​are in the query variable '<%=laneID%>' .

Please help me keep the values.

I think jQuery might help in this case. but if there is any other solution for this, please suggest.

Thanks a ton.

+4
source share
1 answer

Romi

Cannot access SELECT with

 document.getElementById("laneid").text = '<%=laneID%>'; document.getElementById("laneid").value = '<%=laneID%>'; 

You should use instead.

 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 easiest.

Using Struts, you can also populate and select a drop-down list using the <html:select> and <html:optionsCollection> . Check this.

+1
source

All Articles