Selectize.js manually add some items

前端 未结 6 1772
生来不讨喜
生来不讨喜 2020-12-15 15:39

I want add some items to a selectized input after user clicks on a button. The input data are loaded via Ajax. When I call addItem(value) no thing happens. But if I try to t

相关标签:
6条回答
  • 2020-12-15 16:07

    Try This

    var $select = $(document.getElementById('Your-element-id'));
    var selectize = $select[0].selectize;
    selectize.addOption({value: '2', text: 'test'});
    selectize.addItem('2');
    
    0 讨论(0)
  • 2020-12-15 16:08

    You can add options like this:

    var $select = $(document.getElementById('mySelect')).selectize(options);
    var selectize = $select[0].selectize;
    selectize.addOption({value: 1, text: 'whatever'});
    selectize.refreshOptions();
    

    This only adds the option as possible selection. Now you can use addItem to add the new option to the list:

    selectize.addItem(1);
    

    This does not need a refresh function. You do not need to use "refreshOptions" if you add the new option immediately.

    0 讨论(0)
  • 2020-12-15 16:19

    This plugin does not attempt to load an item metadata from the server. You need to first add an option using addOption() method. Next, you can use addItem().

    v.selectize.addOption({value:13,text:'foo'}); //option can be created manually or loaded using Ajax
    v.selectize.addItem(13); 
    
    0 讨论(0)
  • 2020-12-15 16:23
    $('#id').selectize({
       create: function(input,callback){
          $.ajax({
               url: "",
               type: "POST",
               data: {value : input},
                  success: function(res) {
                       callback({value: res, text: input});
                  }
          });
       }
    });
    
    0 讨论(0)
  • 2020-12-15 16:24

    If you want to be more flexible then you can use the length like this.

    var $select = $(document.getElementById('Your-ID'));        
    var selectize = $select[0].selectize;
    var count = selectize.items.length + 1;
    selectize.addOption({ value: count, text: 'value-here' });
    selectize.addItem(count);
    
    0 讨论(0)
  • 2020-12-15 16:25

    Try this.

     $('.select-ajax-city').each(function() {
        if (this.selectize) {
            for(x=0; x < 10; ++x){
                this.selectize.addOption({value:x, text: x});
            }
        }
    });  
    
    0 讨论(0)
提交回复
热议问题