如何使用selectize.js在运行时设置新添加的选项的值

时间:2017-09-07 06:39:44

标签: jquery selectize.js

如何在运行时设置新添加选项的值。目前在值中选择选项名称。我正在使用下面的代码。

 $(document).ready(function () { 
         $('#dept').selectize({  
            create: true, sortField: 'text' 
        });
}); 

enter image description here

2 个答案:

答案 0 :(得分:0)

使用Selectize API中提供的createItemupdateOption方法添加新选项并自定义其值。查看this fiddle.

答案 1 :(得分:0)

Its working for me like
$(function(){
        $('#desig').selectize({
            create:function (input, callback){
                $.ajax({
                    url: "<?php  echo $this->url(null, array('controller' => 'employee-detail', 'action' => 'add-new-desig-ajax'));?>",
                    type: 'POST',
                    data:{'designation':input},
                    success: function (result) {
                        if (result) {
//                            console.log('sdfasf',result);
                            callback({ value: result.id, text: input });
                        }
                    }
                });
            }
        });
    }); 

I have taken help from here
https://stackoverflow.com/questions/24366365/creating-an-item-if-not-already-exists-in-selectize-js-select-box-and-ajax-updat/46096111#46096111