How to set a value for a selectize.js input?

后端 未结 11 2099
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 23:51

I have a form from which I would like to copy some default values into the inputs. The form inputs are using the selectize.js plugin. I would like to set some of the form

11条回答
  •  北海茫月
    2020-12-14 00:33

    I was having this same issue - I am using Selectize with Rails and wanted to Selectize an association field - I wanted the name of the associated record to show up in the dropdown, but I needed the value of each option to be the id of the record, since Rails uses the value to set associations.

    I solved this by setting a coffeescript var of @valueAttr to the id of each object and a var of @dataAttr to the name of the record. Then I went through each option and set:

     opts.labelField = @dataAttr
     opts.valueField = @valueAttr
    

    It helps to see the full diff: https://github.com/18F/C2/pull/912/files

提交回复
热议问题