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

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

    Try this:

    $("#autoship_option option[selected]").removeAttr("selected");
    
    0 讨论(0)
  • 2020-11-30 22:09

    I am not sure if this applies to the older version of chosen,but now in the current version(v1.4.1) they have a option $('#autoship_option').chosen({ allow_single_deselect:true }); This will add a 'x' icon next to the name selected.Use the 'x' to clear the 'select' feild.

    PS:make sure you have 'chosen-sprite.png' in the right place as per the chosen.css so that the icons are visible.

    0 讨论(0)
  • 2020-11-30 22:10
    var config = {
          '.chosen-select'           : {},
          '.chosen-select-deselect'  : {allow_single_deselect:true},
          '.chosen-select-no-single' : {disable_search_threshold:10},
          '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
          '.chosen-select-width'     : {width:"95%"}
        }
        for (var selector in config) {
          $(selector).chosen(config[selector]);
        }
    

    Use above config and apply class='chosen-select-deselect' for manually deselect and set the value empty

    or if you want deselect option in all combo then apply config like below

    var config = {
      '.chosen-select'           : {allow_single_deselect:true},//Remove if you do not need X button in combo
      '.chosen-select-deselect'  : {allow_single_deselect:true},
      '.chosen-select-no-single' : {disable_search_threshold:10},
      '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
      '.chosen-select-width'     : {width:"95%"}
    }
    for (var selector in config) {
      $(selector).chosen(config[selector]);
    }
    
    0 讨论(0)
  • 2020-11-30 22:11

    This is more effective than find.

    $('select').children('option').first().prop('selected', true)
    $('select').trigger("chosen:updated");
    
    0 讨论(0)
  • 2020-11-30 22:12

    In Chrome version 49

    you need always this code

    $("select option:first").prop('selected', true)

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

    i think you have to assign a new value to the select, so if you want to clear your selection you probably want to assign it to the one that has value = "". I think that you will be able to get it by assigning the value to empty string so .val('')

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