[英]How to store ajax response into a jquery variable being used in html in textbox
这是我的代码要点: https : //gist.github.com/anuragk098/3e11673136325b5e5b1859bde11f2117这是我的页面: http : //abachelorskitchen.com/
在第一个文本框中,我从jquery函数获取所有城市:
var months = [<?=$city;?>];
$('#tbCountries1').autocomplete({
source : months,
minLength: 0,
minChars: 0,
max: 12,
autoFill: true,
mustMatch: true,
matchContains: false,
scrollHeight: 220,
formatItem: function(data, i, total) {
if ( data[0] == months[new Date().getMonth()] )
return false;
return data[0];
}
}).on('focus', function(event) {
var self = this;
$(self).autocomplete( "search", "");;
});
和onblur我正在调用ajax函数来获取该城市的所有区域。 但是现在我想要另一个jQuery函数中的所有区域,该区域的文本框将显示结果。
Ajax功能:
function getarea(city){
$.ajax({
url: '<?php echo base_url()?>Home/getArea',
type: 'POST',
data: {id: city},
success: function(data) {
area = data;
}
});
}
现在,区域变量包含类似“ area1”,“ area2”的字符串。 从我获取区域的另一种jquery方法:
$(document).ready(function() {
$('.select2').select2();
});
$( function() {
var availableTags = [area];
$( "#tbCountries" ).autocomplete({
source: availableTags
});
} );
检查要点以获取所有代码和页面链接,以更好地理解我想要实现的功能。
如果要根据第一个输入中的选择来自动完成第二个输入,则可以像对第一个输入(tbCountries1)一样为第二个输入(tbCountries)初始化一个自动完成功能,但是将autocomplete source选项作为一个函数提供,例如所以:
$('#tbCountries1').autocomplete(
{
/* Other options here */
source: function(request, response)
{
/* Fetch the the data using request.term and value from tbCountries1 */
var options = getarea($("#tbCountries1").val());
/* Then return the array to the callback */
response(options);
}
});
有关源选项的更多信息,请参见: http : //api.jqueryui.com/autocomplete/#option-source
编辑:
好。 我将尝试进一步解释我的想法。 对于第一个输入字段,您可以添加支持的所有城市的列表。 您已定义变量
var months = [<?=$city;?>];
插入到第一个自动完成中是这样的:
source : months,
对于第二个输入,您可以创建类似的自动完成功能,但是不必传递该月份变量,而可以使用一个函数根据所选城市动态创建一系列地点。 因此,对于第二个输入,您将执行以下操作:
$("#tbCountries").autocomplete(
{
/* Other options here */
source: function(request, response)
{
/* Here we pass the selected city, the searched locality and */
/* the callback for the autocomplete */
getarea($("#tbCountries1").val(), request.term, response);
}
});
然后,您可以按以下方式编辑getArea函数:
function getarea(city, searched, response)
{
$.ajax(
{
url: '<?php echo base_url()?>Home/getArea',
type: 'POST',
data:
{
id: city
},
success: function(options)
{
/* If the options is not an array, you need to convert it to one here */
var optionsFiltered = $.ui.autocomplete.filter(options, searched);
response(optionsFiltered);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.