当用户输入时,Select2下拉列表允许用户使用新值
select2组件可以配置为接受新值,如在Select2下拉列表中询问但是允许用户使用新值?
你可以在http://jsfiddle.net/pHSdP/646/看到它,代码如下:
$("#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键,新值将仅添加到列表中。
是否可以将select2组件设置为在使用类型时接受此新值并保留select2。 (就像普通的html输入标签一样,通过点击屏幕上的某个位置保留你输入的值)
我发现select2有select2-blur
事件,但我找不到获取这个新值并将其添加到列表的方法?!
添加属性selectOnBlur: true,
似乎对我selectOnBlur: true,
。
编辑:很高兴它也适合你!
我使用的是Select2 4.0.3并且必须添加两个选项:
tags: true, selectOnBlur: true,
并且能够与现有选择一起提交多个新选择:
select2({tags: true, selectOnBlur: true, multiple: true})