Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drop down list help  RSS feed

 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp form that has a drop down option for a user to select. I would like to know how to have a pre-selected option that has been retrieved from my database or provide a default option if none has been selected by the user. My form allows a user to update a record in my database and what I need to achieve is when a user is updating an existing record they do not have to touch the drop down box if the do not need to change that option. Here is my current form below
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic HTML: place the selected attribute on the option you wish tone selected.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Basic HTML: place the selected attribute on the option you wish tone selected.

My problem is that the selected option is always going to be different because when updating the form the preselected value can differ from item to item.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the value captured in s scoped variable passed to the JSP? If not, that's the first step.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Do you have the value captured in s scoped variable passed to the JSP? If not, that's the first step.


Yes I have a valued captured and I have tested to make sure it is actually being retrieved.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. But before we proceed, you need to fix your option elements. They all have the same value, and that makes no sense. The value of the option is what will be submitted as the value of the selected when submitted. So having them all the same value means you'll have no way to know which one was chosen.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Cool. But before we proceed, you need to fix your option elements.
That was an error I had overlooked and here is the updated code. My problem now is dropdown option is empty then it just shows a blank space and if its filled in, it shows 2 similar values.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the first option is automatically selected, and it's empty. So it's doing exec fly what you told it to. What is the purpose of that first empty option?

Before going any further, you need to reformat your HTML to have proper formatting and indentation. I had a hard time even finding the select element.

Causing a specific option to be selected is as easy as making sure that the selected attribute is placed on the correct option.

As a starter, of the selected value is in a scoped variable named whatever:

This places the selected attribute on the option only if whatever matches the value (in this case 3).
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, I have tried the code you have given me and that has heklwith the code.


Bear Bibeault wrote:



As a starter, of the selected value is in a scoped variable named whatever:




 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!