【问题标题】:jquery ui autocomplete: remote source problemjquery ui自动完成:远程源问题
【发布时间】:2011-09-06 02:48:08
【问题描述】:

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

怎么了?

【问题讨论】:

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


    【解决方案1】:
        $( ".query-input" ).autocomplete({
                minLength: 3,
                dataType: 'json',
                source: "/search.json",
                select: function( event, ui ) { window.location = ui.item.url }
    })
    

    你应该提到 dataType 为 'json'

    【讨论】:

      【解决方案2】:

      我不认为html_safe 会用双引号字符包装该值。

      编辑:原来html_safe 确实添加了双引号字符。但是,您的 Firebug 跟踪中的 JSON 无效,并且与您问题中的代码不匹配:

      [
          {
              type: "Utente",
              label: "Luca XXXX",
              url: "lvh.me:3000/users/4dde465add53e04e5c000001"; <-- syntax error
          }, {
              type: "Domanda",
              label: "Luca asdas adsfdsfdsf sdsd",
              url: "lvh.me:3000/questions/luca-asdas-adsfdsfdsf-sdsd"; <-- syntax error
          },
      ]
      

      url 的值后面不能有分号。

      【讨论】:

      【解决方案3】:

      问题是错过的回调。自动完成需要一个回调参数才能正常运行。 正确的search.json代码如下:

      <%= "#{params[ :callback ]}(" if params[:callback] -%>
      [<% @results.each_with_index do |r, i| %>
        <% if r.is_a? User %>
          {
            type: "Utente",
            label: <%= r.name.to_json.html_safe %>,
            url: "<%= user_url(r.id) %>"
          }
        <% elsif r.is_a? Question %>
          {
            type: "Domanda",
            label: <%= r.text.to_json.html_safe %>,
            url: "<%= question_url(r.slug) %>"
          }
        <% elsif r.is_a? Topic %>
          {
            type: "Argomento",
            label: <%= r.name.to_json.html_safe %>,
            url: "<%= topic_path(r.slug) %>"
          }
        <% end %>
        <%= "," unless i == @results.count - 1 %>
      <% end %>]
      <%= ")" if params[:callback] -%>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-16
        • 2017-06-19
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多