【问题标题】:Select2 callback function when [duplicate]Select2回调函数[重复]
【发布时间】:2015-04-27 08:57:30
【问题描述】:

我们使用选择二要标记的输入。我们需要触发自动保存只要选择一个标签。有当选择一个新的或现有的标记用于选择二回调? P>

    $(".tagstypeahead").select2({

        tags: true,
        tokenSeparators: [','],
        createSearchChoice: function (term) {

            return {
                id: $.trim(term),
                text: $.trim(term) // + ' (new tag)' - Adds new tag to the end here
            };

        },
        ajax: {
            url: '/tags/typeahead.json',
            dataType: 'json',
            data: function(term, page) {
                return {
                    q: term
                };
            },
            results: function(data, page) {
                return {
                    results: data
                };
            }
        },

        // Take default tags from the input value
        initSelection: function (element, callback) {

            var data = [];

            function splitVal(string, separator) {
                var val, i, l;
                if (string === null || string.length < 1) return [];
                val = string.split(separator);
                for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
                return val;
            }

            $(splitVal(element.val(), ",")).each(function () {
                data.push({
                    id: this,
                    text: this
                });
            });

            callback(data);
        }

    });

【问题讨论】:

    标签: javascript jquery ajax jquery-select2


    【解决方案1】:

    我不认为select2 有更改回调,但你可以使用jQuery 来实现

    在该元素上绑定更改事件

                    $elem.unbind('change');
                    $elem.change(function (e) {
                        var tags = $elem.select2('data');
                        // code  
                    });
    

    【讨论】:

      猜你喜欢
      • 2012-08-26
      • 2017-08-06
      • 2013-06-24
      • 2013-03-13
      • 2018-07-14
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多