Reset select value to default

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

I have select box


                        
    
提交评论

  • 2020-11-27 04:04

    I have select box

    <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>
    

    I have also reset button, here default (selected) value is "b", suppose I select "c" and after I need resert select box value to default, how to make this using Angular2?

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

    You can use the data attribute of the select element

    <select id="my_select" data-default-value="b">
        <option value="a">a</option>
        <option value="b" selected="selected">b</option>
        <option value="c">c</option>
    </select>
    

    Your JavaScript,

    $("#reset").on("click", function () {
        $("#my_select").val($("#my_select").data("default-value"));
    });
    

    http://jsfiddle.net/T8sCf/10/

    UPDATE


    If you don't know the default selection and if you cannot update the html, add following code in the dom ready ,

    $("#my_select").data("default-value",$("#my_select").val());
    

    http://jsfiddle.net/T8sCf/24/

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

    You can also reset the values of multiple SELECTs and then trigger the submit button.

    Please see it here in action:

    Live Example:

    $(function(){
        $("#filter").click(function(){
            //alert('clicked!');
            $('#name').prop('selectedIndex',0);
            $('#name2').prop('selectedIndex',0);
            $('#submitBtn').click();
        });
    });
    
    0 讨论(0)
  • 2020-11-27 04:06

    A simple way that runs is

     var myselect = $("select.SimpleAddAClass");
     myselect[0].selectedIndex = 0;
     myselect.selectmenu("refresh");
    
    0 讨论(0)
  • 2020-11-27 04:08
    $("#reset").on("click", function () {
        $("#my_select").val('b');//Setting the value as 'b'
    });
    
    0 讨论(0)
  • 提交回复
    热议问题