Select2 dropdown but allow new values by user?

前端 未结 9 1442
灰色年华
灰色年华 2020-11-27 10:09

I want to have a dropdown with a set of values but also allow the user to \"select\" a new value not listed there.

I see that select2 supports this if you are using

9条回答
  •  攒了一身酷
    2020-11-27 10:24

    Just for the sake of keep the code alive, I'm posting @rrauenza Fiddle's code from his comment.

    HTML

    
    

    jQuery

    $("#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'}]
    });
    

提交回复
热议问题