如何清除选定的值selectize.js下拉列表
我有一个selectize.js下拉菜单,我必须清除所选的值。
我试过了 :
var selectize = $("#optionNetFlow")[0].selectize; selectize.clear();
如另一个问题中所建议的如何以编程方式设置selectize.js选项列表 。 但它给出了一个错误,
Uncaught TypeError: Cannot read property 'selectize' of undefinedmessage: "Cannot read property 'selectize' of undefined"stack: (...)get stack: function () { [native code] }arguments: nullcaller: nulllength: 0name: ""prototype: StackTraceGetterconstructor: function () { [native code] }__proto__: Object__proto__: function Empty() {}set stack: function () { [native code] }__proto__: Error
当我把它改为:
var selectize = $("#optionNetFlow").selectize; selectize.clear();
我给出了错误:
TypeError: undefined is not a functionmessage: "undefined is not a function"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error VM1253:3(anonymous function) VM1253:3InjectedScript._evaluateOn VM1251:732InjectedScript._evaluateAndWrap VM1251:665InjectedScript.evaluate VM1251:579
我在这里做错了什么?
我终于在这里找到了答案Selectize.js Demos
对我有用的是:
var $select = $('#optionNetFlow').selectize(); var control = $select[0].selectize; control.clear();
我缺少的是var $select = $('#optionNetFlow').selectize();
在应用上述问题答案中提供的解决方案之前。
现在我要在控制台中获得所有function:
试试这个: – http://jsfiddle.net/adiioo7/2gnq1ruv/204/
JS: –
jQuery(function ($) { var $select = $('#input-tags').selectize({ persist: false, create: true }); $("#btnClear").on("click", function () { var selectize = $select[0].selectize; selectize.clear(); }); });
试试这个,
$("#optionNetFlow")[0].selectize.clear();
$(document).on('click', 'div.selectize-input div.item', function(e) { var select = $('#services').selectize(); var selectSizeControl = select[0].selectize; // 1. Get the value var selectedValue = $(this).attr("data-value"); // 2. Remove the option select[0].selectize.removeItem(selectedValue); // 3. Refresh the select select[0].selectize.refreshItems(); select[0].selectize.refreshOptions(); });
这不会从选择中删除项目,只需从选定的选项中删除它。