Select2 dropdown but allow new values by user?

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

    Since many of these answers don't work in 4.0+, if you are using ajax, you could have the server add the new value as an option. So it would work like this:

    1. User searches for value (which makes ajax request to server)
    2. If value found great, return the option. If not just have the server append that option like this: [{"text":" my NEW option)","id":"0"}]
    3. When the form is submitted just check to see if that option is in the db and if not create it before saving.

提交回复
热议问题