Select menu not being restored when Back button used

后端 未结 6 843
予麋鹿
予麋鹿 2021-01-31 19:47

Consider a web page that has a select menu with a JavaScript event handler tied to the menu\'s onchange event that when fired reloads the page with a new query string (using the

6条回答
  •  太阳男子
    2021-01-31 20:25

    The snippet from @frakhoffy worked for me, but broke normal select behavior in Chrome. When hitting the page without a selected option, the select was blank even though there is not a blank option in the select. I ended up going with:

    $(document).ready(function () {
      $('select').each(function () {
        var select = $(this);
        var selectedValue = select.find('option[selected]').val();
    
        if (selectedValue) {
          select.val(selectedValue);
        } else {
          select.prop('selectedIndex', 0);
        }
      });
    });
    

提交回复
热议问题