This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes How to retain option menu selection upon form submission Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to retain option menu selection upon form submission" Watch "How to retain option menu selection upon form submission" New topic
Author

How to retain option menu selection upon form submission

Bob Owens
Greenhorn

Joined: Sep 26, 2013
Posts: 2
I am currently learning Java in school and we are currently working on some JSP.

It is a basic assignment, but I am having trouble retaining the selected option on the option menu when the user clicks the submit button. It always resets the option menu to the first option listed when the user clicks on submit.

The frequency_value is also only retaining the first value of the row's option menu. It looks like it doesn't care about the other 3 rows option menus.

Ignore the fact that I am outputting the frequency_value into the amount column, I just wanted to see the value the variable was retaining.
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Hey Bob...Welcome to the Ranch...
In HTML you make an option selected in a select box by using the keyword "selected".

The above code will select "Monthly" option by default in UI. You have to modify your code such that you identify the selected value at server side and add the "selected" attribute for the appropriate option
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 158
^ OP already has selected attribute for `option` element in his code. From above,



Bob, which browser are you using?

Edit: Maybe test your markup in jsfiddle and see if you get the result that you expected.
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Yep... How I missed that... :)
The problem here is OP is hardcoding "selected" to the first option every time.. Instead it should be programatically determined to which option he had to set the attribute. Nothing to do with browser isnt it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Tarun Bolla is correct. If you want the select element to retain the vale selected in the previous response, the selected attribute must be applied to the corresponding option.

If the OP were using the modern EL, that's a really easy thing to do with the ternary operator. But as obsolete scriptlets are being used, an equivalent scriptlet expression is necessary.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bob Owens
Greenhorn

Joined: Sep 26, 2013
Posts: 2
I talked to a java programmer at work today and he helped me out.

Here is my finished code.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain option menu selection upon form submission