Select2 dropdown but allow new values by user?

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

    var text = 'New York Mills';
    var term = 'new york mills';
    return text.localeCompare(term)===0;
    

    In most cases we need to compare values with insensitive register. And this code will return false, which will lead to the creation of duplicate records in the database. Moreover String.prototype.localeCompare () is not supported by browser Safary and this code will not work in this browser;

    return this.text.localeCompare(term)===0;
    

    will better replace to

    return this.text.toLowerCase() === term.toLowerCase();
    

提交回复
热议问题