2013-07-08 42 views
-1

我有问题,我有一个选择框,但是如何显示选定的值?选项在红宝石中选择:查看选定的值

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

请帮忙;)

回答

0

设置 '选择' HTML属性

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

这将选择第一个选项

+0

我需要一个提交按钮吗?并重新加载页面以显示选定的值? – user2432672

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