问题
I have a selectize.js dropdown & I have to clear the selected value .
I have tried :
var selectize = $("#optionNetFlow")[0].selectize;
selectize.clear();
as suggested in an another question How do I set the selectize.js option List programmatically. But it gives an error,
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() {}<function scope>set stack: function () { [native code] }__proto__: Error

when I change it to :
var selectize = $("#optionNetFlow").selectize;
selectize.clear();
I gives error:
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

what I am doing wrong here?
回答1:
I finally found the answer here Selectize.js Demos
What works for me is:
var $select = $('#optionNetFlow').selectize();
var control = $select[0].selectize;
control.clear();
what I was missing var $select = $('#optionNetFlow').selectize();
before applying the solution provided in above question's answer.
Now I am to get all the functions in console like :

回答2:
Try this out:- 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();
});
});
回答3:
Try this,
$("#optionNetFlow")[0].selectize.clear();
回答4:
All other answers either clear a single selectize or need a specific reference to the selectize in the moment of it's creation.
The solution below, on the other hand, works for any number of selectize elements you have inside any form; you just need to specify the desired form:
$('form').find('.selectized').each(function(index, element) { element.selectize && element.selectize.clear() })
The rationale is that Selectize keeps the original element in the DOM (hiding it), adds a reference to the selectize on the .selectize
property of the DOM element and adds a CSS class selectized
to it.
So the solution finds all the elements that have the CSS class selectized
, loops through them and calls element.selectize.clear()
.
回答5:
$(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();
});
This do not remove the item from the select, just remove it from the selected options.
回答6:
Or if you have multi select, and do want to restore selected items in the drop-down list (hide selected set to true).
var selectize = $("#select-item").selectize;
//clone array
var items = selectize.items.slice(0);
for (var i in items) {
selectize.removeItem(items[i]);
}
selectize.refreshOptions();
来源:https://stackoverflow.com/questions/26923751/how-to-clear-a-selected-value-selectize-js-dropdown