首页 > 解决方案 > 使用 ajax 引导标签输入和预先输入

问题描述

我正在使用 Bootstraptagsinputtypeahead,当我以这种方式使用它时效果很好:

$(document).ready(function() {
    $('.tagsinput-typeahead').tagsinput({
        typeahead: {
            source: $.getJSON('/ajax/tags.php'),
            afterSelect: function() {
                this.$element[0].value = '';
            }
        }
    });
});

但我想将它与ajax一起使用,但它只显示第一个字母。

$(document).ready(function() {    
    jQuery('.tagsinput-typeahead').tagsinput({
        typeahead : {
            source: function (query, process) {
                return jQuery.get('/ajax/tags.php?query=' + query, function(data){  
                // return process(data);              
                });
            }
        }
    });
});

即使我回来process(data);,它也不会起作用。

此外,当我单击一个建议时,该字母也会出现在输入字段中。

图片

我的tags.php样子是这样的:

$json = [];
$json[] = "test";
$json[] = 'Washington';
$json[] = 'London';
$json[] = 'Germany';
$json[] = 'Denmark';
echo json_encode($json); 

有任何想法吗?

标签: jqueryajaxtwitter-bootstrapbootstrap-typeaheadbootstrap-tags-input

解决方案


推荐阅读