为什么'autofocus: true'不适用于 Rails 中的collection_select,而它适用于 text_field=



我有一个表单,我尝试自动对焦于collection_select,但这不起作用。它适用于其他表单项,例如number_field和text_field。我不明白为什么?

正常工作的代码:

<tr>
  <td><%= form.label :invoice_number %></td>
  <td><%= form.number_field :invoice_number, value: 100, autofocus: true %></td>
</tr>

不工作的代码:

<tr>
  <td><%= form.label :customer_id %></td>
  <td><%= form.collection_select :customer_id, Customer.all, :id, :name, autofocus: true %></td>
</tr>

>number_field只有一个options哈希参数:number_field(object_name, method, options = {})

collection_select有两个:collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

因此,您需要这样称呼它:

<%= form.collection_select :customer_id, Customer.all, :id, :name, {}, {autofocus: true} %>

相关内容