Select2 占位符在 rails 应用中无法使用 hidden_field
Select2 placeholder not working in rails app with hidden_field
哈姆尔:
.form-group
= f.hidden_field :gender, :id => 'select2', :style => "width:100%; height: 40px"
JS:
$("#select2").select2({
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.text.localeCompare(term) === this.text;
}).length === 0) {
return {
id: term,
text: term
};
}
console.log('SELECT2', $("#select2").data.text);
},
placeholder: "Enter custom gender or select one below",
multiple: false,
data: [{
id: 'male',
text: 'male'
}, {
id: 'female',
text: 'female'
}, {
id: 'custom',
text: 'custom'
}]
});
Select2 正在做我现在需要的一切,但占位符没有显示在我的视图中。任何帮助,非常感谢。
我为此编写了一个小咖啡脚本,它可以执行所有正常的自动完成操作以及预填充数据
$(document).ready ->
$('.select2').each (i, e) =>
select = $(e)
options =
placeholder: select.data('placeholder')
minimumInputLength: 1
if select.hasClass('ajax') # only add ajax functionality if this class is present
options.ajax =
url: select.data('source')
quietMillis: 100
dataType: 'json'
data: (term) ->
q: term
results: (data) ->
results: data.resources
more: false
options.dropdownCssClass = "bigdrop"
options.initSelection = (element, callback) ->
callback({ text: element.attr('data-value') })
select.select2(options)
而我的隐藏字段是这样的
hidden_field_tag(:search, '', id: 'res_select', class: 'select2 ajax form-control select-overide', style: 'width: 100%;', data: { source: "/products/autocomplete", placeholder: 'Search for a name' }, :value=> params["search"], "data-value" => params['search'])
试试这个,它会起作用。
上面的回答很好。我通过在占位符填充的空白初始选项前加上更少的代码解决了这个问题:
$('.compare-search').prepend('<option/>').val(function(){return $('[selected]',this).val() ;});
哈姆尔:
.form-group
= f.hidden_field :gender, :id => 'select2', :style => "width:100%; height: 40px"
JS:
$("#select2").select2({
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.text.localeCompare(term) === this.text;
}).length === 0) {
return {
id: term,
text: term
};
}
console.log('SELECT2', $("#select2").data.text);
},
placeholder: "Enter custom gender or select one below",
multiple: false,
data: [{
id: 'male',
text: 'male'
}, {
id: 'female',
text: 'female'
}, {
id: 'custom',
text: 'custom'
}]
});
Select2 正在做我现在需要的一切,但占位符没有显示在我的视图中。任何帮助,非常感谢。
我为此编写了一个小咖啡脚本,它可以执行所有正常的自动完成操作以及预填充数据
$(document).ready ->
$('.select2').each (i, e) =>
select = $(e)
options =
placeholder: select.data('placeholder')
minimumInputLength: 1
if select.hasClass('ajax') # only add ajax functionality if this class is present
options.ajax =
url: select.data('source')
quietMillis: 100
dataType: 'json'
data: (term) ->
q: term
results: (data) ->
results: data.resources
more: false
options.dropdownCssClass = "bigdrop"
options.initSelection = (element, callback) ->
callback({ text: element.attr('data-value') })
select.select2(options)
而我的隐藏字段是这样的
hidden_field_tag(:search, '', id: 'res_select', class: 'select2 ajax form-control select-overide', style: 'width: 100%;', data: { source: "/products/autocomplete", placeholder: 'Search for a name' }, :value=> params["search"], "data-value" => params['search'])
试试这个,它会起作用。
上面的回答很好。我通过在占位符填充的空白初始选项前加上更少的代码解决了这个问题:
$('.compare-search').prepend('<option/>').val(function(){return $('[selected]',this).val() ;});