How to reset a select element with jQuery

后端 未结 15 1844
清歌不尽
清歌不尽 2020-12-24 10:37

I have


                        
    
提交评论

  • 2020-12-24 11:00

    if none of those solutions didn't work for you, try adding after

    .trigger( "change" );
    

    ex.

    $("#baba").val("").trigger( "change" );
    

    or

    $("#baba").val(false).trigger( "change" );
    

    or

    $("#baba option").prop("selected", false).trigger( "change" );
    

    or

    $('#baba').prop('selectedIndex',-1).trigger( "change" );
    

    or

    $('#baba option:first').prop('selected',true).trigger( "change" );
    
    0 讨论(0)
  • 2020-12-24 11:03

    In your case (and in most use cases I have seen), all you need is:

    $("#baba").val("");
    

    Demo.

    0 讨论(0)
  • 2020-12-24 11:08

    Edit: Old fashioned way,

    document.getElementById('baba').selectedIndex = 0;
    

    Try below and it should work for your case,

    $('#baba option:first').prop('selected', true);
    

    DEMO

    0 讨论(0)
  • 2020-12-24 11:13
    $('#baba').prop('selectedIndex',-1);
    
    0 讨论(0)
  • 2020-12-24 11:16

    The best javascript solution I've found is this

    elm.options[0].selected="selected";
    
    0 讨论(0)
  • 提交回复
    热议问题