当用户输入时,Select2下拉列表允许用户使用新值 [英] Select2 dropdown allow new values by user when user types

查看:642
本文介绍了当用户输入时,Select2下拉列表允许用户使用新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将select2组件配置为接受新值,如 Select2下拉列表但允许用户使用新值?

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

您可以在以下位置查看: http://jsfiddle.net/pHSdP/646/ 代码如下:

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'
    }]
});

问题是,如果您输入新值并按Enter键,新值将仅添加到列表中,或按Tab键。

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

使用类型并离开select2时,是否可以将select2组件设置为接受此新值。 (就像普通的html输入标记一样,通过点击屏幕上的某个位置保留你输入的值)

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)

我发现select2有 select2-blur 事件但是我找不到获取这个新值的方法并将其添加到列表中?!

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?!

推荐答案

添加属性 selectOnBlur:true,似乎对我有用。

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

编辑:很高兴它也适合你!

glad it worked for you as well!

这篇关于当用户输入时,Select2下拉列表允许用户使用新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆