admin管理员组文章数量:1332383
I have a jQuery UI Autoplete which is being populated by a third party web service. The autoplete is working correctly, however when I select an item from the list the autoplete input field goes blank.
The autoplete is used to search an address, once selected the address gets split into its ponents 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 autoplete field.
The web service returns an array of objects each containing key value pairs named pretty much as above.
Originally the JS code looked like:
$("#FullAddress").autoplete({
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. i.e. in the image above once one was selected the "123 te" disappears.
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.
$("#FullAddress").autoplete({
source: "URL",
dataType: "JSONP",
autoFocus: true,
create: function () {
$(this).data('ui-autoplete')._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 autoplete 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
I have a jQuery UI Autoplete which is being populated by a third party web service. The autoplete is working correctly, however when I select an item from the list the autoplete input field goes blank.
The autoplete is used to search an address, once selected the address gets split into its ponents 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 autoplete field.
The web service returns an array of objects each containing key value pairs named pretty much as above.
Originally the JS code looked like:
$("#FullAddress").autoplete({
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. i.e. in the image above once one was selected the "123 te" disappears.
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.
$("#FullAddress").autoplete({
source: "URL",
dataType: "JSONP",
autoFocus: true,
create: function () {
$(this).data('ui-autoplete')._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 autoplete 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
Share Improve this question asked Mar 25, 2014 at 22:09 JakeJake 1,2272 gold badges29 silver badges46 bronze badges1 Answer
Reset to default 9If you add event.preventDefault();
as the first line in the select function that should stop the field emptying:
select: function (event, ui) {
event.preventDefault();
$("#FullAddress").val(ui.item['FullAddress']);
...
}
本文标签: javascriptJQuery Autocomplete not filling input with selected itemStack Overflow
版权声明:本文标题:javascript - JQuery Autocomplete not filling input with selected item - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742298418a2449191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论