【问题标题】:Jquery autocomplete not filling textboxJquery自动完成不填充文本框
【发布时间】:2017-04-27 12:58:13
【问题描述】:

我有一个自动完成功能,可以过滤本地 json 以将结果返回到文本框,但是在选择位置时,自动完成功能不会填充文本框。我试图根据文档包含一个select 方法,但它破坏了我的功能。我应该在我的代码库中的什么位置包含select

自动完成功能:

  $('#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
              };

    });

数据

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"}
    ]

【问题讨论】:

    标签: javascript jquery jquery-ui autocomplete


    【解决方案1】:

    在您的情况下,选择可以放在源之前或之后,只要它作为属性保留在自动完成对象中即可。你可以按照这个例子:

    $('#autocomplete').autocomplete({
        minLength: 1,
        source: function (request, response) { ... },
        select: function (event, ui) {
            event.preventDefault();
            // [...] your code here
            return false;
        }
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) { ... };
    

    【讨论】:

    • 太棒了!如果它工作正常,请接受这个答案。
    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2016-03-22
    相关资源
    最近更新 更多