jquery ui 自动完成 : remote source problem

标签 jquery json ruby-on-rails-3 jquery-ui autocomplete

我正在使用 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/

相关文章:

ruby - RSpec Git Bash Windows——缺少颜色?

ruby-on-rails-3 - 轨道中的对象负载过早

javascript - 在 Rails 应用程序中显示图形和图表?

javascript - 范围 slider - 向下箭头跟随拇指

Javascript日期时间时间只给出 "Uncaught TypeError: undefined is not a function"

php - 在 for 循环中将两个条目粘在一起

json - 需要将目标文件中字典中的值替换为源文件中字典中的值

javascript - 垂直滑动导航菜单不起作用

javascript - 重置 setInterval() 的定时器

javascript - 循环 JSON 并打印 html