Reset select value to default

前端 未结 23 1585
暗喜
暗喜 2020-11-27 03:52

I have select box


                        
    
提交评论

  • 2020-11-27 04:21
    $("#reset").on("click", function () {
        $('#my_select').prop('selectedIndex',0);
    });
    
    0 讨论(0)
  • 2020-11-27 04:22

    Why not use a simple javascript function and call it on onclick event?

    function reset(){
    document.getElementById("my_select").selectedIndex = 1; //1 = option 2
    }
    
    0 讨论(0)
  • 2020-11-27 04:22
    <select id="my_select">
        <option value="a">a</option>
        <option value="b" selected="selected">b</option>
        <option value="c">c</option>
    </select>
    
    <div id="reset">
        reset
    </div>
    
    $("#reset").on("click", function () {
        $('#my_select').prop('selectedIndex',0);
    });
    

    This is a simple way for your question. only one line answer.

    0 讨论(0)
  • 2020-11-27 04:22

    If you looking to reset, try this simply:

    $('element option').removeAttr('selected');
    

    To set desire value, try like this:

    $(element).val('1');
    
    0 讨论(0)
  • 提交回复
    热议问题