我有这个:
# _form.html.erb
f.text_field :c, data: {autocomplete_source: @correlations.all.map { |x| x.name }}<br>
f.text_field :n, data: {autocomplete_source: @customers.all.map { |x| x.note}}
// Coffee script
$('#launch_c').autocomplete source: $('#launch_c').data('autocomplete-source')
$('#launch_n').autocomplete source: $('#launch_n').data('autocomplete-source')
我需要变量@customers取决于相关性的选择...... 任何人都可以帮助我吗?
答案 0 :(得分:1)
如果我理解正确,这不是一件容易的事。基本上你需要这样的东西:
结果可能看起来像这个Jquery代码:
$('#launch_c').autocomplete({
source: $('#launch_c').data('autocomplete-source'),
change: function(event, ui){
var correlation_name = ui.item.value;
var data = $('#launch_n').data('autocomplete-source')[correlation_name];
$('#launch_n').autocomplete({ source: data });
}
});
这需要#launch_n有一个data-autocomplete-source
嵌套的JSON,如下所示:
{
"correlation1_name": ["customer1_name", "customer2_name", "customer3_name"],
"correlation2_name": ["customer4_name", "customer5_name", "customer6_name"],
"correlation3_name": ["customer7_name", "customer8_name", "customer9_name"],
// etc.
}