简体   繁体   English

使用JSON时,jQuery自动完成功能不起作用

[英]JQuery Autocomplete didn't work when using JSON

I want to make an autocomplete text field to display the name and get the value when i select one of them. 我想创建一个自动完成文本字段来显示名称,并在选择其中一个时获取值。

Im following the tutorial from this site 我正在关注本网站的教程

here 这里

and success. 和成功。 the working code is like this: 工作代码如下:

var data = [
        { value: "AL", label: "Alabama" },
        { value: "AK", label: "Alaska" },
        { value: "AZ", label: "Arizona" }
];
$(function() {
        $("#autocomplete2").autocomplete({
            source: data,
            focus: function(event, ui) {
                event.preventDefault();
                $(this).val(ui.item.label);
            },
            select: function(event, ui) {
                event.preventDefault();
                $(this).val(ui.item.label);
                $("#autocomplete2-value").val(ui.item.value);
            }
        });
    });

I want to replace the var data written above with my JSON. 我想用JSON替换上面写的var数据。 here is what my JSON look like 这是我的JSON的样子

[{"value":"1","label":"Gambir"},{"value":"2","label":"Kebon Kelapa"},{"value":"3","label":"Petojo Utara"}]

i want to do this 我想做这个

var data = [
        here would be data from my json,
        the JSON url: app.base_url+'adm/pengiriman/pengiriman_kodepos'
        how to write code to retrieve JSON in here?
];
$(function() {
    $("#autocomplete2").autocomplete({
        source: data,
        focus: function(event, ui) {
            event.preventDefault();
            $(this).val(ui.item.label);
        },
        select: function(event, ui) {
            event.preventDefault();
            $(this).val(ui.item.label);
            $("#autocomplete2-value").val(ui.item.value);
        }
    });
});

but, what is the proper way to do that? 但是,这样做的正确方法是什么? i try modify the source from this: 我尝试从中修改源:

source: data,

become this: 变成这个:

source: function (request, response) {
    $.getJSON(app.base_url+'adm/pengiriman/pengiriman_kodepos', function (data) {
         response($.map(data, function (value, key) {
             return {
                 label: value,
                 value: key
             };
          }));
     });
},

or this: 或这个:

source: function(request, response) {
              $.ajax({
                url: app.base_url+'adm/pengiriman/pengiriman_kodepos',
                dataType: "json",
                data: { q: request.term },
                success: function(data) {
                  response($.map(data, function(value,key) {
                    return { label:value.label , value: value.value }
                  }));
                }
              });
            },

or this: 或这个:

source: app.base_url+'adm/pengiriman/pengiriman_kodepos',

still have no luck.. im pretty new to JQuery and also JSON thanks for any help.. 仍然没有运气..我对JQuery和JSON还是很新的,感谢您的帮助。

Not sure what you mean. 不明白你的意思。 Just replace the json with your json 只需将json替换为您的json

An ajax request in asynchronous, so you have to use a callback and create the autocomplete ui in that. 一个异步的ajax请求,因此您必须使用回调并在其中创建自动完成ui。 Please see the code below. 请参见下面的代码。 Not tested, so you may have to tweak the code, but this is how it can be done. 未经测试,因此您可能需要调整代码,但这是可以完成的方法。

function getData(responseCallback) {
  $.getJSON(app.base_url + 'adm/pengiriman/pengiriman_kodepos', function(data) {
    responseCallback(data);
  });
}


function createAutocomplete(data) {
  $("#autocomplete2").autocomplete({
    source: data,
    focus: function(event, ui) {
      event.preventDefault();
      $(this).val(ui.item.label);
    },
    select: function(event, ui) {
      event.preventDefault();
      $(this).val(ui.item.label);
      $("#autocomplete2-value").val(ui.item.value);
    }
  });
}

getData(createAutocomplete);

I have created an example here : https://jsbin.com/subihohuje/1/edit?html,output 我在这里创建了一个示例: https : //jsbin.com/subihohuje/1/edit?html,输出

It gets data from : https://jsonblob.com/api/9ae66445-f11b-11e6-901d-295e753a8fe1 它从以下位置获取数据: https : //jsonblob.com/api/9ae66445-f11b-11e6-901d-295e753a8fe1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM