简体   繁体   English

JQuery 自动完成未使用所选项目填充输入

[英]JQuery Autocomplete not filling input with selected item

I have a jQuery UI Autocomplete which is being populated by a third party web service.我有一个由第三方 Web 服务填充的 jQuery UI 自动完成功能。 The autocomplete is working correctly, however when I select an item from the list the autocomplete input field goes blank.自动完成工作正常,但是当我从列表中选择一个项目时,自动完成输入字段变为空白。

The autocomplete is used to search an address, once selected the address gets split into its components which fill out the rest of the form.自动完成用于搜索地址,一旦被选中,该地址就会被拆分成它的组成部分,这些组成部分填写了表单的其余部分。

So I have the following input field ids: #FullAddress, #AddressLine1,#AddressLine2, #AddressSuburb,#AddressState, #AddressPostcode with #FullAddress being the autocomplete field.所以我有以下输入字段 ID: #FullAddress, #AddressLine1,#AddressLine2, #AddressSuburb,#AddressState, #AddressPostcode #FullAddress是自动完成字段。

The web service returns an array of objects each containing key value pairs named pretty much as above. Web 服务返回一个对象数组,每个对象都包含与上述名称相似的键值对。

Originally the JS code looked like:最初的 JS 代码如下所示:

$("#FullAddress").autocomplete({
    source: "URL",
    dataType: "JSONP",
    autoFocus: true,
    select: function (event, ui) {
        $("#FullAddress").val(ui.item['FullAddress']);
        $("#AddressLine1").val(ui.item['Line1']);
        $("#AddressLine2").val(ui.item['Line2']);
        $("#AddressSuburb").val(ui.item['Suburb']);
        $("#AddressState").val(ui.item['State']);
        $("#AddressPostcode").val(String(ui.item['Postcode']));
    }
});

Which contacted the server and was returning results, but they were not being displayed in the dropdown:哪个联系了服务器并返回了结果,但它们没有显示在下拉列表中:

在此处输入图片说明

Clicking any option filled out all fields with address data (you just couldn't see which address you selected) apart from #FullAddress which gets blanked out.单击任何选项会填写所有包含地址数据的字段(您只是看不到您选择的地址),除了#FullAddress会被清空。 ie in the image above once one was selected the "123 te" disappears.即在上图中,一旦被选中,“123 te”就会消失。

I discovered the following to add to the create event which fixed the dropdown display issue, but did not fix the fact that the #FullAddress field gets blanked no matter what happens.我发现以下内容添加到create事件中,该事件修复了下拉显示问题,但没有解决#FullAddress字段无论发生什么都会被清空的事实。

    $("#FullAddress").autocomplete({
        source: "URL",
        dataType: "JSONP",
        autoFocus: true,
        create: function () {
            $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
                return $("<li>")
                    .append('<a>' + item.FullAddress + '</a>')
                    .appendTo(ul);
            };
        },
        select: function (event, ui) {
            $("#FullAddress").val(ui.item['FullAddress']);
            $("#AddressLine1").val(ui.item['Line1']);
            $("#AddressLine2").val(ui.item['Line2']);
            $("#AddressSuburb").val(ui.item['Suburb']);
            $("#AddressState").val(ui.item['State']);
            $("#AddressPostcode").val(String(ui.item['Postcode']));
        }
    }); 

在此处输入图片说明

Does any one know any other reasons why the autocomplete input field would be emptying?有没有人知道自动完成输入字段会清空的任何其他原因?

It does not appear to care whether or not an item was selected, no matter what happens on blur the field empties.它似乎并不关心是否选择了一个项目,无论在模糊字段清空时发生什么。

Thanks谢谢

If you add event.preventDefault();如果添加event.preventDefault(); as the first line in the select function that should stop the field emptying:作为应该停止字段清空的 select 函数中的第一行:

select: function (event, ui) {
    event.preventDefault();
    $("#FullAddress").val(ui.item['FullAddress']);
    ...
}

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

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