How do I reset a jquery-chosen select option with jQuery?

前端 未结 21 673
一个人的身影
一个人的身影 2020-11-30 21:42

I have tried numerous things and nothing seems to be working.

I am using jQuery and Chosen plugin.

Methods I have tried:

var select = jQuery(         


        
相关标签:
21条回答
  • 2020-11-30 22:18

    Exactly i had the same requirement. But resetting the drop down list by setting simply empty string with jquery won't work. You should also trigger update.

    Html:
    <select class='chosen-control'>
    <option>1<option>
    <option>2<option>
    <option>3<option>
    </select>
    
    Jquery:
    $('.chosen-control').val('').trigger('liszt:updated');
    

    sometimes based on the version of chosen control you are using, you may need to use below syntax.

    $('.chosen-control').val('').trigger("chosen:updated");
    

    Reference: How to reset Jquery chosen select option

    0 讨论(0)
  • 2020-11-30 22:20

    Try this to reload updated Select box with Latest Chosen JS.

    $("#form_field").trigger("chosen:updated");
    

    http://harvesthq.github.io/chosen/

    It will Updated chosen drop-down with new loaded select box with Ajax.

    0 讨论(0)
  • 2020-11-30 22:20
    jQuery("#autoship_option option:first").attr('selected', true);
    
    0 讨论(0)
  • 2020-11-30 22:23
    $('#autoship_option').val('').trigger('liszt:updated');
    

    and set the default option value to ''.

    It has to be used with chosen updated jQuery available at this link: https://raw.github.com/harvesthq/chosen/master/chosen/chosen.jquery.min.js.

    I spent one full day to find out at the end that jquery.min is different from chosen.jquery.min

    0 讨论(0)
  • 2020-11-30 22:24

    If you are using chosen, a jquery plugin, then to refresh or clear the content of the dropdown use:

    $('#dropdown_id').empty().append($('< option>'))
    
    dropdown_id.chosen().trigger("chosen:updated")
    

    chosen:updated event will re-build itself based on the updated content.

    0 讨论(0)
  • 2020-11-30 22:26

    The first option should sufice: http://jsfiddle.net/sFCg3/

    jQuery('#autoship_option').val('');
    

    But you have to make sure you are runing this on an event like click of a button or ready or document, like on the jsfiddle.

    Also make sure that theres always a value attribute on the option tags. If not, some browsers always return empty on val().

    Edit:
    Now that you have clarifyed the use of the Chosen plugin, you have to call

    $("#autoship_option").trigger("liszt:updated");
    

    after changing the value for it to update the intereface.

    http://harvesthq.github.com/chosen/

    0 讨论(0)
提交回复
热议问题