最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select2 dropdown allow new values by user when user types - Stack Overflow

programmeradmin2浏览0评论

The select2 component can be configured to accept new values, as ask in Select2 dropdown but allow new values by user?

And you can see it at: / the code is as below:

$("#tags").select2({
    createSearchChoice: function (term, data) {
        if ($(data).filter(function () {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id: term,
                text: term
            };
        }
    },
    multiple: false,
    data: [{
        id: 0,
        text: 'story'
    }, {
        id: 1,
        text: 'bug'
    }, {
        id: 2,
        text: 'task'
    }]
});

The problem is that the new value is only added to the list if you enter new value and press enter, or press tab.

Is it possible to set the select2 component to accept this new value when use types and leave the select2. (Just as normal html input tag which keeps the value which you are typing when you leave it by clicking some where on the screen)

I found that the select2 has select2-blur event but I don't find a way to get this new value and add it to list?!

The select2 component can be configured to accept new values, as ask in Select2 dropdown but allow new values by user?

And you can see it at: http://jsfiddle.net/pHSdP/646/ the code is as below:

$("#tags").select2({
    createSearchChoice: function (term, data) {
        if ($(data).filter(function () {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id: term,
                text: term
            };
        }
    },
    multiple: false,
    data: [{
        id: 0,
        text: 'story'
    }, {
        id: 1,
        text: 'bug'
    }, {
        id: 2,
        text: 'task'
    }]
});

The problem is that the new value is only added to the list if you enter new value and press enter, or press tab.

Is it possible to set the select2 component to accept this new value when use types and leave the select2. (Just as normal html input tag which keeps the value which you are typing when you leave it by clicking some where on the screen)

I found that the select2 has select2-blur event but I don't find a way to get this new value and add it to list?!

Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Sep 2, 2014 at 5:34 Alireza FattahiAlireza Fattahi 45.5k16 gold badges131 silver badges184 bronze badges 2
  • 2 Adding attribute selectOnBlur: true, seems to work for me – Jason Commented Sep 2, 2014 at 5:38
  • It worked! Please see it at: jsfiddle.net/pHSdP/647 Send the answer so I can mark it as correct! – Alireza Fattahi Commented Sep 2, 2014 at 5:46
Add a comment  | 

3 Answers 3

Reset to default 11

Adding attribute selectOnBlur: true, seems to work for me.

Edit: glad it worked for you as well!

I am using Select2 4.0.3 and had to add two options:

tags: true,
selectOnBlur: true,

This worked for me

And to be able to submit multiple new choices together with the existing ones:

select2({tags: true, selectOnBlur: true, multiple: true})
发布评论

评论列表(0)

  1. 暂无评论