Jquery tokeninput,第一次搜索不起作用

时间:2013-12-11 10:17:36

标签: jquery jquery-tokeninput

我对tokeninput有一个奇怪的问题......

    $("[name='business_list']").tokenInput( {{businessList|raw}}, {
        hintText: "{{ 'business.findBusiness'|trans }}",
        noResultsText: "{{'no_results'|trans({},'app')|capitalize}}",
        searchingText: "{{'searching'|trans({},'app')|capitalize}}",
        prePopulate: {{businesses|raw}},
        onReady: function(){
            $("[name='business_list']").siblings(".token-input-list").find("#token-input-").focus();
        },
        onResult: function(items){
            items.push({
                value: "aaa",
                label: "<span>aaa</span>"
            })

            console.log(items);
            //return items;
        }
    });

它的工作原理如下: 例如,当我第一次输入'a'时,它显示'No results'(但在控制台日志中我可以看到他找到了7个对象)。当我删除'a'并再次输入它时它工作正常。但是......当我输入例如'z'时,它将再次相同。它显示'没有结果',但我可以看到它找到了4个对象,在删除和再次输入后,它显示了它们...... 有什么想法吗?

没有'onResult'功能它工作正常,但我想为任何结果添加一个对象,但是这样做不行......

1 个答案:

答案 0 :(得分:0)

问题已解决,代码:

$("[name='business_list']").tokenInput( {{businessList|raw}}, {
            hintText: "{{ 'business.findBusiness'|trans }}",
            noResultsText: "{{'no_results'|trans({},'app')|capitalize}}",
            searchingText: "{{'searching'|trans({},'app')|capitalize}}",
            prePopulate: {{businesses|raw}},
            onReady: function(){
                $("[name='business_list']").siblings(".token-input-list").find("#token-input-").focus();
            },
            onResult: function(items){
                var value = $('#token-input-').val();
                items.push({
                    label: "<span class='new-business'><img src='{{ asset('bundles/cloudcrm/images/business.png') }}' style='margin-right: 5px;'>{{ 'deals.newDeal.addBusiness'|trans({},'crm')|capitalize }}<strong>" + " " + value + "</strong></span>",
                    id: "new",
                    value: value
                });
                return items;
            },
            onAdd: function(item){
                if( $(item.label).hasClass("new-business") == true ){
                    $("[name='business_list']").tokenInput("remove", item);
                    $(".modal-business-add").modal('show');
                }
            }
        });