• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retain option menu selection upon form submission

 
Bob Owens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^ 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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Owens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I talked to a java programmer at work today and he helped me out.

Here is my finished code.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic