Select2 Event for creating a new tag

前端 未结 3 1204
闹比i
闹比i 2021-01-01 15:58

I\'m using the jQuery Select2 (v4) plugin for a tag selector.

I want to listen for when a new tag is created in the select element and fire an ajax request to store

3条回答
  •  盖世英雄少女心
    2021-01-01 16:29

    Another workaround. Just insert it to the beginning:

               }).on('select2:selecting', function (evt) {
    
                 var stringOriginal = (function (value) {
                    // creation of new tag
                    if (!_.isString(value)) {
                        return  value.html();
                    }
                    // picking existing
                    return value;
                })(evt.params.args.data.text);
                ........
    

    It relies on underscore.js for checking if it's string or not. You can replace _.isString method with whatever you like.

    It uses the fact that when new term is created it's always an object.

提交回复
热议问题