【问题标题】:Bootstrap Typeahead and Tagsinput doesn't live togetherBootstrap Typeahead 和 Tagsinput 不能同时存在
【发布时间】:2016-08-10 09:42:54
【问题描述】:

我制作了这个脚本并且完美运行:

$('.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);
                    }
                })
            }
        });

如何在标签输入中实现我的脚本?

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

$('.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

我能做什么?

我尝试了一些东西,但它们都不起作用...我不想使用其他组件..

谢谢

【问题讨论】:

  • 什么是进程()???
  • 是一个 typeahead fn,用于在其滚动中推送项目。你如何实现 typeahead 和 tagsinput 一起?使用 ajax 后调用..

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


【解决方案1】:
$('.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"
    ]
}

对于标签输入

$('.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 格式返回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多