我正在使用 jQuery UI 自动完成。
当我使用局部变量作为源时,它可以工作。
var json = [
{type: "Utente",label: "Luca XXXX",url: "http://lvh.me:3000/users/4dde465add53e04e5c000001"},
{type: "Domanda",label: "Luca asdas adsfdsfdsf sdsd",url: "http://lvh.me:3000/questions/luca-asdas-adsfdsfdsf-sdsd"},
];
但是当我从另一个文件返回相同的源时,它不起作用。我在 firebug 中找到了 JSON 对象,它似乎与我的本地变量 json 相同。
代码如下:
$( ".query-input" ).autocomplete({
minLength: 3,
source: "/search.json",
select: function( event, ui ) { window.location = ui.item.url }
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a href=" + item.url + ">"+ item.label +"</a><span>" + item.type + "</span>" )
.appendTo( ul );
};
这是 search.json.erb 文件:
[<% @results.each do |r| %>
<% if r.is_a? User %>
{type: "Utente",label: <%= r.name.to_json.html_safe %>,url: <%= user_url(r.id).to_json.html_safe %>},
<% elsif r.is_a? Question %>
{type: "Domanda",label: <%= r.text.to_json.html_safe %>,url: <%= question_url(r.slug).to_json.html_safe %>},
<% elsif r.is_a? Topic %>
{type: "Argomento",label: <%= r.name.to_json.html_safe %>,url: <%= topic_path(r.id).to_json.html_safe %>},
<% end %>
<% end %>]
出了什么问题?
最佳答案
$( ".query-input" ).autocomplete({
minLength: 3,
dataType: 'json',
source: "/search.json",
select: function( event, ui ) { window.location = ui.item.url }
})
您应该将 dataType 指定为“json”
关于jquery ui 自动完成 : remote source problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198604/