Javascript 成功执行ajax后,不会显示使用typeahead建议的Jquery自动完成
我使用typeahead.js为多重输入放置标记。标签输入功能正常,但其自动完成建议没有出现。有没有办法纠正这个问题 我已经尝试了与我的问题相关的大多数解决方案,这些解决方案已经出现在这个网站上,但目前仍然无法显示自动完成建议。我总是被成功的ajax响应所困扰,就是这样 我的jquery:Javascript 成功执行ajax后,不会显示使用typeahead建议的Jquery自动完成,javascript,php,jquery,ajax,typeahead.js,Javascript,Php,Jquery,Ajax,Typeahead.js,我使用typeahead.js为多重输入放置标记。标签输入功能正常,但其自动完成建议没有出现。有没有办法纠正这个问题 我已经尝试了与我的问题相关的大多数解决方案,这些解决方案已经出现在这个网站上,但目前仍然无法显示自动完成建议。我总是被成功的ajax响应所困扰,就是这样 我的jquery: <script> $("#s_to").tagsinput({ tagClass: 'uk-badge', typeaheadjs: { s
<script>
$("#s_to").tagsinput({
tagClass: 'uk-badge',
typeaheadjs: {
source: function(query) {
console.log(query);
url = "<?php echo base_url(); ?>index.php/<?php echo $loc_pts; ?>/ajax_email";
var s_to = extractLast(query);
ajax_status = "fail";
$.ajax({
url: url,
method: "POST",
data: {
s_to: s_to
},
async: false,
dataType: "json",
success: function(json){
return json.s_to;
}
});
}
}
});
</script>
$(“#s#u to”).标记输入({
tagClass:‘英国徽章’,
typeaheadjs:{
来源:函数(查询){
console.log(查询);
url=“index.php//ajax\u电子邮件”;
var s_to=提取最后一次(查询);
ajax_status=“fail”;
$.ajax({
url:url,
方法:“张贴”,
数据:{
s_to:s_to
},
async:false,
数据类型:“json”,
成功:函数(json){
将json.s_返回到;
}
});
}
}
});
我的意见:
<input required type="text" name="s_to" id="s_to" class="controls uk-autocomplete-results" value="<?php echo $s_client_email; ?>" autocomplete="on" data-provide="typeaheadjs" />
主要问题是没有在正确的范围内返回数组。您的返回json.s_到
在ajax成功函数中,但需要在父范围中返回值。因此,代码应该是这样的:
$("#s_to").tagsinput({
tagClass: 'uk-badge',
typeaheadjs: {
source: function(query) {
console.log(query);
url = "<?php echo base_url(); ?>index.php/<?php echo $loc_pts; ?>/ajax_email";
var s_to = extractLast(query);
ajax_status = "fail";
var toReturn = [];
$.ajax({
url: url,
method: "POST",
data: {
s_to: s_to
},
async: false,
dataType: "json",
success: function(json) {
toReturn = json.s_to;
}
});
/* This is the correct scope to return the array */
return toReturn;
}
}
});
$(“#s#u to”).tagsinput({
tagClass:‘英国徽章’,
typeaheadjs:{
来源:函数(查询){
console.log(查询);
url=“index.php//ajax\u电子邮件”;
var s_to=提取最后一次(查询);
ajax_status=“fail”;
var toReturn=[];
$.ajax({
url:url,
方法:“张贴”,
数据:{
s_to:s_to
},
async:false,
数据类型:“json”,
成功:函数(json){
toReturn=json.s_to;
}
});
/*这是返回数组的正确范围*/
回归回归;
}
}
});
可能没有错误,但json可能格式错误。请执行console.log(json)
来检查它,或者在浏览器的检查器中检查网络/XHR选项卡。您也可以在这里发布JSON。我为console.log(JSON)输出添加了一个图像链接。我也会记录查询。我尝试过你的方法,但不知怎么的,建议还是没有出来。我甚至在返回之前使用.map进行了一些调整,这样它只会将s_返回到ajax数据,但仍然是一样的。我可以猜出还有什么不对。如果你可以在小提琴上重现问题,那么有人可能会进一步帮助你。
$("#s_to").tagsinput({
tagClass: 'uk-badge',
typeaheadjs: {
source: function(query) {
console.log(query);
url = "<?php echo base_url(); ?>index.php/<?php echo $loc_pts; ?>/ajax_email";
var s_to = extractLast(query);
ajax_status = "fail";
var toReturn = [];
$.ajax({
url: url,
method: "POST",
data: {
s_to: s_to
},
async: false,
dataType: "json",
success: function(json) {
toReturn = json.s_to;
}
});
/* This is the correct scope to return the array */
return toReturn;
}
}
});