简体   繁体   English

如何避免在select2 gem中选择提示

[英]how to avoid prompt as selected in select2 gem

I have used select2-rails gem. 我用过select2-rails gem。 When it is applied, the prompt is also selectable. 应用时,也可以选择提示。 How to avoid prompt as selected? 如何避免选择提示?

You can also do something like this on the element without having to specify placeholder in Javascript. 您也可以在元素上执行类似的操作,而无需在Javascript中指定占位符。

<%= f.collection_select :method, Model.all, :id, :name,
{include_blank: true}, data: {placeholder: 'Choose Model'}, class:
"select2"%>

assuming you have initialized this element with select2 somewhere in your javascript files. 假设您已在javascript文件中的某处使用select2初始化此元素。

使用'placeholder'选项,如下所示:

$(element).select2(placeholder: 'select one option', ** other options here);

IN YOURVIEW.HTML.SLIM 在YOURVIEW.HTML.SLIM

select.form-control#js__multiselect multiple="multiple" name='your_name[]' value="#{params[:your_value]}"
  = options_from_collection_for_select(Model.all, :id, :value, params[:your_params])

# :id, :value are what values you want to show from your Model.
# params[:your_params], which is array is your selected value.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM