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

前端 未结 21 672
一个人的身影
一个人的身影 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:27

    You can try this to reset (empty) drop down

     $("#autoship_option").click(function(){
                $('#autoship_option').empty(); //remove all child nodes
                var newOption = $('<option value=""></option>');
                $('#autoship_option').append(newOption);
                $('#autoship_option').trigger("chosen:updated");
            });
    
    0 讨论(0)
  • 2020-11-30 22:27

    from above solutions, Nothing worked for me. This worked:

    $('#client_filter').html(' '); //this worked

    Why is that? :)

    $.ajax({ 
            type: 'POST', 
            url: xyz_ajax,
            dataType: "json",
            data : { csrfmiddlewaretoken: csrftoken,
                    instancess : selected_instances }, 
            success: function(response) { 
                //clear the client DD       
                $('#client_filter').val('').trigger('change') ; //not working
                $('#client_filter').val('').trigger('chosen:updated') ; //not working
                $('#client_filter').val('').trigger('liszt:updated') ; //not working
                 $('#client_filter').html(' '); //this worked
                 jQuery.each(response, function(index, item) {
                      $('#client_filter').append('<option value="'+item.id+'">' + item.text + '</option>');
                    });                 
               $('#client_filter').trigger("chosen:updated");
            }
          });
         } 
    
    0 讨论(0)
  • 2020-11-30 22:30

    Setting the select element's value to an empty string is the correct way to do it. However, that only updates the root select element. The custom chosen element has no idea that the root select has been updated.

    In order to notify chosen that the select has been modified, you have to trigger chosen:updated:

    $('#autoship_option').val('').trigger('chosen:updated');
    

    or, if you're not sure that the first option is an empty string, use this:

    $('#autoship_option')
        .find('option:first-child').prop('selected', true)
        .end().trigger('chosen:updated');
    

    Read the documentation here (find the section titled Updating Chosen Dynamically).


    P.S. Older versions of Chosen use a slightly different event:

    $('#autoship_option').val('').trigger('liszt:updated');
    
    0 讨论(0)
  • 2020-11-30 22:31

    HTML:

    <select id="autoship_option" data-placeholder="Choose Option..." 
            style="width: 175px;" class="chzn-select">
        <option value=""></option>
        <option value="active">Active Autoship</option>
    </select>
    <button id="rs">Click to reset</button>
    

    JS:

    $('#rs').on('click', function(){
        $('autoship_option').find('option:selected').removeAttr('selected');
    });
    

    Fiddle: http://jsfiddle.net/Z8nE8/

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

    If you need to have first option selected by no matter of its value (sometimes first option value is not empty) use this:

    $('#autoship_option').val($('#autoship_option option:first-child').val()).trigger("liszt:updated");
    

    It will get value of first option and set it as selected then trigger update on Chosen. So it work like reset to first option on list.

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

    I use this:

    $('idSelect').val([]);

    tested on IE, Safari & Firefox

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