-1

i have the problem, that i have a select box, but how can i display the selected value?

<select name="please">
     <option value="<% $lcost.sup %>">Super</option>
     <option value="<% $lcost.supp %>">Super Plus</option>
     <option value="<% $lcost.supe %>">Super E 10</option>
     <option value="<% $lcost.lpg %>">LPG</option>
     <option value="<% $lcost.diesel%>">Diesel</option>
     </select>

please help ;)

2 Answers2

1
suppose if @object is the form object and 'car' is the attribute then you should put condition on each option tag: ---

<select name="please">
     <option value="<% $lcost.sup %>" "<% @object.car == $lcost.sup ? 'selected' : '' %>">Super</option>
     <option value="<% $lcost.supp %>" "<% @object.car == $lcost.supp ? 'selected' : '' %>">Super Plus</option>
     <option value="<% $lcost.supe %>" "<% @object.car == $lcost.supe ? 'selected' : '' %>">Super E 10</option>
     <option value="<% $lcost.lpg %>" "<% @object.car == $lcost.lpg ? 'selected' : '' %>">LPG</option>
     <option value="<% $lcost.diesel%>" "<% @object.car == $lcost.diesel ? 'selected' : '' %>">Diesel</option>
     </select>

read HTML doc for showing selected values in a select box. http://www.w3schools.com/tags/att_option_selected.asp

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
0

Set 'selected' html attribute

<select name="please">
 <option value="<% $lcost.sup %>" selected="selected">Super</option>
 <option value="<% $lcost.supp %>">Super Plus</option>
 <option value="<% $lcost.supe %>">Super E 10</option>
 <option value="<% $lcost.lpg %>">LPG</option>
 <option value="<% $lcost.diesel%>">Diesel</option>
</select>

this will select first option