How to clear a selected value in Selectize.js dropdown?

后端 未结 6 1703
离开以前
离开以前 2021-02-05 00:39

I have a selectize.js dropdown and I have to clear the selected value .

I have tried this (as suggested in another question):

var selectize = $(\"#option         


        
6条回答
  •  面向向阳花
    2021-02-05 01:12

    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().

提交回复
热议问题