【问题标题】:Jquery UI 1.12 Cannot set property '_renderItem' of undefinedJquery UI 1.12 无法设置未定义的属性“_renderItem”
【发布时间】:2016-11-27 16:23:07
【问题描述】:

Jquery 的 UI 库向我抛出了一个错误。我正在尝试调用自动完成功能,但是我收到以下错误:

Cannot set property '_renderItem' of undefined

谁能看出我哪里出错了?我正在努力调试它。

自动完成脚本

$('#autocomplete').autocomplete({
    minLength: 1,
    source: suggestion,
    focus: function(event, ui) {
                $('#autocomplete').val(ui.item.name);
                return false;
            },

    select: function(event, ui) {

        $('#autocomplete').val(ui.item.name);

                return false;
            }
        })

        .data( "ui-autocomplete" )._renderItem = function( ul, item ) {

                return $( "<li></li>" )
                    .data( "ui-autocomplete-item", item )

                    .append( "<a>" + item.name + "</a>" )
                    .appendTo( ul );
            };

      })

我从另一个stackoverflow answer 和这个JSfiddle 获取了这段代码,但也许这些例子不再可行。

这是我正在使用的数据。最终我只想返回 Citycountry 值。

  var suggestion =

  [
  {"id":"1","name":"Goroka","city":"Goroka","country":"Papua New Guinea","iata":"GKA","icao":"AYGA","latitude":"-6.081689","longitude":"145.391881","altitude":"5282","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"2","name":"Madang","city":"Madang","country":"Papua New Guinea","iata":"MAG","icao":"AYMD","latitude":"-5.207083","longitude":"145.7887","altitude":"20","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"3","name":"Mount Hagen","city":"Mount Hagen","country":"Papua New Guinea","iata":"HGU","icao":"AYMH","latitude":"-5.826789","longitude":"144.295861","altitude":"5388","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"4","name":"Nadzab","city":"Nadzab","country":"Papua New Guinea","iata":"LAE","icao":"AYNZ","latitude":"-6.569828","longitude":"146.726242","altitude":"239","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"5","name":"Port Moresby Jacksons Intl","city":"Port Moresby","country":"Papua New Guinea","iata":"POM","icao":"AYPY","latitude":"-9.443383","longitude":"147.22005","altitude":"146","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ,
  {"id":"6","name":"Wewak Intl","city":"Wewak","country":"Papua New Guinea","iata":"WWK","icao":"AYWK","latitude":"-3.583828","longitude":"143.669186","altitude":"19","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
  ]

layout.jade

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    link(rel='stylesheet', href='//code.jquery.com/ui/1.12.0-rc.1/themes/smoothness/jquery-ui.css')
  body
    block content
  script(src='//code.jquery.com/jquery-1.11.3.js')
  script(src='//code.jquery.com/ui/1.12.0-rc.1/jquery-ui.js')
  script(src='/javascripts/autocomplete.js')

自动完成输入

form(method='post', action='/add', class='form')
  .form-group
    input.form-control(type='text', name='destination' id='autocomplete')
  .form-group
    input.form-control(type='text', name='month')
  button.btn.btn-default(type='submit') Add Destination

【问题讨论】:

    标签: javascript jquery jquery-ui autocomplete


    【解决方案1】:

    如果未找到附加自动完成功能的元素,则会出现您指定的问题。

    您提供供参考的 JSfiddle 缺少包含 jquery 本身

    根据您的要求检查以下工作小提琴。只需输入城市的第一个字母,自动完成就会显示过滤后的响应。

            $(function() {
    
                 var suggestion =
    
      [
      {"id":"1","name":"Goroka","city":"Goroka","country":"Papua New Guinea","iata":"GKA","icao":"AYGA","latitude":"-6.081689","longitude":"145.391881","altitude":"5282","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ,
      {"id":"2","name":"Madang","city":"Madang","country":"Papua New Guinea","iata":"MAG","icao":"AYMD","latitude":"-5.207083","longitude":"145.7887","altitude":"20","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ,
      {"id":"3","name":"Mount Hagen","city":"Mount Hagen","country":"Papua New Guinea","iata":"HGU","icao":"AYMH","latitude":"-5.826789","longitude":"144.295861","altitude":"5388","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ,
      {"id":"4","name":"Nadzab","city":"Nadzab","country":"Papua New Guinea","iata":"LAE","icao":"AYNZ","latitude":"-6.569828","longitude":"146.726242","altitude":"239","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ,
      {"id":"5","name":"Port Moresby Jacksons Intl","city":"Port Moresby","country":"Papua New Guinea","iata":"POM","icao":"AYPY","latitude":"-9.443383","longitude":"147.22005","altitude":"146","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ,
      {"id":"6","name":"Wewak Intl","city":"Wewak","country":"Papua New Guinea","iata":"WWK","icao":"AYWK","latitude":"-3.583828","longitude":"143.669186","altitude":"19","timezone":"10","dst":"U","tz":"Pacific/Port_Moresby"}
      ]
    
          $('#autocomplete').autocomplete({
        minLength: 1,
        source: function(request, response) {            
                var data = $.grep(suggestion, function(value) {
                    return value.city.substring(0, request.term.length).toLowerCase() == request.term.toLowerCase(); // Here the suggestion array is filtered based on what the user has typed. User input will be captured in the request.term
                });            
    
                response(data); // this will return the filtered data which will be attached with the input box.
        }
    
       }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    
                    return $( "<li></li>" )
                        .data( "ui-autocomplete-item", item )
    
                        .append( "<a>" + item.city + "," + item.country + "</a>" )
    
                        .appendTo( ul ); // here we are creating and appending appending element based on the response object we got after filtering
                };
    
          });
    

    http://jsfiddle.net/gomu04jx/

    【讨论】:

    • 非常感谢!你能简单解释一下代码中发生了什么吗?
    • @RhysEdwards 我已经更新了一些代码解释 cmets,希望这会有所帮助。
    • 对于 Jquery-ui 1.12,它再次更改为:.autocomplete("instance")._renderItem。参考:jqueryui.com/autocomplete/#custom-data
    【解决方案2】:

    试试这个?

    $('#autocomplete').autocomplete({
        minLength: 1,
        source: suggestion,
        focus: function(event, ui) {
                    $('#autocomplete').val(ui.item.name);
                    return false;
                },
    
        select: function(event, ui) {
    
            $('#autocomplete').val(ui.item.name);
    
                    return false;
                }
            })
    
            .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    
                    return $( "<li></li>" )
                        .data( "ui-autocomplete-item", item )
    
                        .append( "<a>" + item.name + "</a>" )
                        .appendTo( ul );
                };
    

    【讨论】:

    • 请您也分享其他来源项目,例如您使用的html
    • 嘿 Jacques,我之前确实尝试过,但不幸的是它没有用。我已经添加了我的布局源以及我想要调用自动完成的 html。我使用翡翠作为我的模板引擎。
    • 仅从您提供的初始代码开始,由于右大括号而导致代码中断。看我的更新。与此同时,我正在测试是否可以让它工作。
    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2022-10-30
    • 2023-02-10
    相关资源
    最近更新 更多