当用户插入正确的值并转义输入字段时,我想自动填充输入字段。这方面的一个例子是https://www.redbus.in/ 我使用 url 字符串发送数据请求,然后接收结果。我想继续发送数据请求并接收结果,即使用户离开了这个领域?然后必须选择带有结果的列表的第一个值。比你帮忙!
我的代码是:
jQuery(document).ready(function(){
jQuery( "#econt_offices_town" ).autocomplete({
minLength: 2,
autoFocus: true,
source: function( request, response ) {
jQuery.ajax({
url: ajaxurl,
dataType: "json",
data: {
action:'handle_ajax',
city: request.term
},
success: function( data ) {
response(jQuery.map(data, function(item) {
return {
label: item.label,
value: item.value,
city_id: item.id,
post_code: item.post_code
};
}));
}, //end of success
}); //end of ajax
}, //end of source
select: function( event, ui ) {
var city_id = ui.item.city_id;
var post_code = ui.item.post_code;
jQuery('#econt_offices_postcode').val(post_code);
jQuery('#office_locator').show(); //show office locator button after the city is selected
jQuery('#econt_offices_postcode, label[for="econt_offices_postcode"], #econt_offices, label[for="econt_offices"]').show();
jQuery.ajax({
url: ajaxurl,
dataType: "json",
data: {
action:'handle_ajax',
office_city_id: city_id,
delivery_type: 'to_office'
},
success: function( data ) {
jQuery('#econt_offices').empty()
jQuery.each(data, function(key, value) {
jQuery('#econt_offices').append(jQuery("<option/>", {
value: value.id,
text: value.value + ' [о.к.:' + value.id + ']'
}));
});
calculate_loading(); //calculate loading cost for shipping to office
} //end of success
}); //end of ajax
}, //end of select
}); //end of #econt_offices_town .autocomplete