Rails select_tag的使用

範例:
現在假設你有一個@bl的陣列資料要顯示
@bl={[:id=1,:my_data=> ' abc' ,:my_rel=> '123' ],[:id=2,:my_data=> 'def' ,:my_rel=> ' 456' ]}

<%=select_tag 'my_id' , options_for_select(@bl.collect{ |b| [b.my_data, b.id,:rel=> b.my_rel] },@selected_id),{:onchange=> 'my_js()' } %>

顯示:

<select id="my_id" name="my_id" onchange="my_js()" >
     <option value="1" rel="123">abc</option>
     <option value="2" rel="456">def</option>
</select>

如果要在option中增加selected,則更換@selected_id即可。

留言

熱門文章