2013-07-08 43 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