开发者

how to select option marked selected in <select> on page refresh (or f5 is pressed)

I am having a <select> with few options on my page, one of the option is marked selected. When the page is loaded first time that options is pre-selected as expected. Now if user chooses another option and refreshes the page by pressing f5 (or clicking refresh). Mozilla and almost all modern browsers selects the options chosen by user not the one marked selected. How can i make sure that on page refresh option marked selected is always pre-selected, not the one user chose before pressing F5;

<select name="Fruit">
    <option value="7336707">Apple</option>
    <option value="9288359">Guava</option>
    <option value="17973开发者_开发百科63" selected="selected">Mango</option>
    <option value="9288359">Grapes</option>
</select>


Accidentally i too was facing the same problem. And solved like below.

$(document).ready(function(){       
   $('select').each(function(){
        $(this).val($(this).find("option[selected=selected]").val());
  });
});


Maybe you can select it with jQuery inside domReady:

$(document).ready(function(){
      $('select[name=fruit]').val($('select[name=fruit] option:selected').val());
});

Can't test this one though. The idea is that if in the html one option is marked as selected, this code sets the select to the value of that option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜