Select2 dropdown but allow new values by user?

前端 未结 9 1398
灰色年华
灰色年华 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:31

    Improvent on @fmpwizard answer:

    //Allow manually entered text in drop down.
    createSearchChoice:function(term, data) {
      if ( $(data).filter( function() {
        return term.localeCompare(this.text)===0; //even if the this.text is undefined it works
      }).length===0) {
        return {id:term, text:term};
      }
    },
    
    //solution to this error: Uncaught TypeError: Cannot read property 'localeCompare' of undefined
    
    0 讨论(0)
  • 2020-11-27 10:31

    I just stumbled upon this from Kevin Brown. https://stackoverflow.com/a/30019966/112680

    All you have to do for v4.0.6 is use tags: true parameter.

    0 讨论(0)
  • 2020-11-27 10:33

    There is a better solution I think now

    simply set tagging to true on the select options ?

    tags: true
    

    from https://select2.org/tagging

    0 讨论(0)
提交回复
热议问题