Selectize.js手动添加一些项目

时间:2013-12-01 09:22:53

标签: javascript jquery selectize.js

我希望在用户点击按钮后为选择的输入添加一些项目。输入数据通过Ajax加载。当我调用addItem(value)时,没有任何事情发生。但是,如果我尝试在输入中键入一些字符串,它会加载数据,然后addItem(value)将起作用。

https://github.com/brianreavis/selectize.js/blob/master/docs/api.md

6 个答案:

答案 0 :(得分:47)

此插件不会尝试从服务器加载项元数据。您需要先使用addOption()方法添加选项。接下来,您可以使用addItem()

v.selectize.addOption({value:13,text:'foo'}); //option can be created manually or loaded using Ajax
v.selectize.addItem(13); 

答案 1 :(得分:26)

您可以添加以下选项:

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

这只会添加选项作为可能的选择。现在,您可以使用addItem将新选项添加​​到列表中:

selectize.addItem(1);

这不需要刷新功能。您不需要使用" refreshOptions"如果你立即添加新选项。

答案 2 :(得分:6)

试试这个。

 $('.select-ajax-city').each(function() {
    if (this.selectize) {
        for(x=0; x < 10; ++x){
            this.selectize.addOption({value:x, text: x});
        }
    }
});  

答案 3 :(得分:3)

尝试一下

$ c++ -std=c++14 try20.cpp
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/shared_ptr.h:52:0,
                 from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:82,
                 from try20.cpp:2:
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*) [with _Tp1 = void; _Tp = A; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/shared_ptr_base.h:1023:4:   required from 'void std::__shared_ptr<_Tp, _Lp>::reset(_Tp1*) [with _Tp1 = void; _Tp = A; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
try20.cpp:14:14:   required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/shared_ptr_base.h:871:39: error: invalid conversion from 'void*' to 'A*' [-fpermissive]
         : _M_ptr(__p), _M_refcount(__p)
                                       ^
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/shared_ptr_base.h:874:4: error: static assertion failed: incomplete type
    static_assert( !is_void<_Tp1>::value, "incomplete type" );

答案 4 :(得分:0)

如果您想更加灵活,则可以使用这样的长度。

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);

答案 5 :(得分:0)

$('#id').selectize({
   create: function(input,callback){
      $.ajax({
           url: "",
           type: "POST",
           data: {value : input},
              success: function(res) {
                   callback({value: res, text: input});
              }
      });
   }
});