Select2 dropdown but allow new values by user?

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

提交回复
热议问题