Bootstrap Typeahead和Tagsinput不在一起

时间:2016-08-10 09:42:54

标签: jquery html bootstrap-typeahead bootstrap-tags-input

我制作了这个剧本并且完美无缺:

$('.js_tags').typeahead({
            minLength: 3,
            source: function (query, process) {
                return $.ajax ({
                    url: '/app/route',
                    type: 'POST',
                    data: 'query='+query,
                    dataType: "json",
                    async: true,
                    success: function (data){
                        process(data);
                    },
                    error: function (request, status, error) {
                        console.log(request.responseText);
                    }
                })
            }
        });

如何在tagsinput中实现我的脚本?

我试过这个,但没有成功:

$('.js_tags').tagsinput({
        typeahead:{
            minLength: 3,
            source: function (query, process) {
            return $.ajax ({
                url: '/app/route',
                type: 'POST',
                data: 'query='+query,
                dataType: "json",
                async: true,
                success: function (data){
                    process(data);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            })
        },
});

我得到了:

  Uncaught TypeError: process is not a function

我该怎么办?

我尝试了一些东西,但没有一个可以工作......我不想使用其他组件..

谢谢

1 个答案:

答案 0 :(得分:0)

$('.typeahead').typeahead({
    source: function (query, process) {
        return $.get('/typeahead', { query: query }, function (data) {
            return process(data.options);
        });
    }
});

使用这样的JSON数据:

{
    "options": [
        "Option 1",
        "Option 2",
        "Option 3",
        "Option 4",
        "Option 5"
    ]
}

对于Tagsinputs

$('.tagsInput').tagsinput({
      minLength: 3,
      typeahead: {                  
          source: function(query) {
            return $.get('/app/route').done(function(data){
              /*if you have add `content-type: application/json` in 
                server response then no need to parse JSON otherwise,
                you will need to parse response into JSON.*/
              return $.parseJSON(data);
            })
          }
      }
 });

注意:数据仅以JSON

返回