在输入框中,当我只输入空格时,它会被接受并显示带有十字图标的空白。
请给我一个解决方案。如何处理。

我的代码:

$("#userfrm #field_value").select2({
        maximumInputLength: 20,
        tags:tags,
        maximumSelectionSize : 10,
        createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
        multiple: true,
    });

最佳答案

您可以侦听select2-selecting事件以拒绝空输入:

$("#userfrm #field_value").select2({
    maximumInputLength: 20,
    tags: tags,
    maximumSelectionSize: 10,
    createSearchChoice: function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id: term,
                text: term
            };
        }
    },
    multiple: true,
})
.on('select2-selecting', function(e) {
    if (!$.trim(e.val)) {
        e.preventDefault();
    }
});

演示:http://plnkr.co/edit/3Z64ZCBiIu8HU5hnAFI7?p=preview

关于javascript - 如何仅忽略select2.js中的空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26422513/

10-09 15:04
查看更多