6

我使用 jquery 链接,如果第一个选择了空白选项,我试图让第二个下拉列表变灰。我假设我需要第二个列表中的空白选项才能锁定它,但我不确定如何添加空白选项。这是选择选项

<%= select_tag :equipment, options_for_select(Equipment.all.collect
                                            { |e| ["#{e.model} - #{e.serialNum}",e.id, 
                                            :class =>"#{e.handReceipt}"]},
                                            html_options = {:id=>'equipment'}) %>

第一个下拉列表允许您选择手收据类型,并且使用 jquery 链接,第二个列表仅显示具有适当手收据属性的记录。

我如何在上面的选择中添加一个空白选项?

编辑-这是我迄今为止尝试过的-

<%= select_tag :equipment, 
     options_for_select(  [["--",""],
                          Equipment.all.collect{ |e|
                          ["#{e.model} - #{e.serialNum}",
                          e.id, :class =>"#{e.handReceipt}"]}],
                          html_options = {:id=>'equipment'}) %>

这会导致列表显示不正确-

<select id="equipment" name="equipment">
   <option value="">--</option>
   <option value="[&quot;M4 - W432156&quot;, 10, {:class=&gt;&quot;Arms Room&quot;}]">[&quot;PSN-13 - 176985&quot;, 1, {:class=&gt;&quot;Commo&quot;}]</option>
</select>

它不显示表中的所有记录,而是显示一个空白选项和第二个选项。

<%= select_tag :equipment, 
     options_for_select(  :include_blank => true,
                          Equipment.all.collect{ |e|
                          ["#{e.model} - #{e.serialNum}",
                          e.id, :class =>"#{e.handReceipt}"]},
                          html_options = {:id=>'equipment'}) %>

导致以下错误 -

C:/Users/Sam/Documents/ruby/btrp/app/views/vehicles/edit.html.erb:19: syntax error, unexpected ',', expecting tASSOC
                          e.id, :class =>"#{e.handReceipt}"]},
4

2 回答 2

15

你的参数options_for_select是错误的,我认为这是正确的方法:

<%= select_tag :equipment,
      options_for_select(Equipment.all.collect { |e|
                           ["#{e.model} - #{e.serialNum}", e.id,
                            { :class =>"#{e.handReceipt}" }]}), 
      :include_blank => true,
      :id => 'equipment' %>

阅读更多:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

于 2013-05-10T19:48:34.427 回答
0

您可以像这样附加空白值。

<%= select_tag "category","<option value=''>Category</option>" +  options_from_collection_for_select(@store_categories, "id", "name",params[:category].to_i)%>
于 2013-12-05T07:57:13.213 回答