How to reselect already selected option

后端 未结 3 1096
礼貌的吻别
礼貌的吻别 2020-12-18 00:52

The title seems confusing but what I want to do is...

I know how to handle only if the user select new option with this - $(\'select\').change(function(){})

3条回答
  •  没有蜡笔的小新
    2020-12-18 01:18

    In instances where nothing should happen when the user selects the already-selected option I suppose it is a "feature" of the DOM rather than a bug to have no Event occur. However, if your code is doing more with option, but if you are willing to specify the length of your list as (in this case)

    jQuery:

    $('select').change(function(){
        var val = $(this).val();
        alert(val);
        $('select').val("");
    });
    

    The only side-effect is that the selection element may now display to the user as three rows rather than one.

    (Credit goes to Kirby L. Wallace for the idea of setting the select's value to "").

提交回复
热议问题