选择ruby中的选项:查看选定的值

时间:2013-07-08 13:34:23

标签: ruby rhomobile

我有问题,我有一个选择框,但我怎样才能显示所选的值?

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

请帮助;)

2 个答案:

答案 0 :(得分: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>
  

阅读HTML文档,以便在选择框中显示所选值。   http://www.w3schools.com/tags/att_option_selected.asp

答案 1 :(得分:0)

设置'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>

这将选择第一个选项