return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Restore dropdown selected option on page refresh? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Restore dropdown selected option on page refresh? - Stack Overflow

programmeradmin2浏览0评论

I have a form like this,

<html>
<body>
<form>
<select name="alphabets" id="alphabets">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D" selected="selected">D</option>
</select>
</form>
</body>
</html>

When I pull the page the first time, option D is selected. If I select option A from the dropdown, and then do a page refresh, I want the dropdown option to go back to D. On page refresh, I want everything to go back to it's initial state. But the dropdown displays option A, or whatever was previously selected.

Any idea how can I make the page display option D on page refresh?

Thanks.

I have a form like this,

<html>
<body>
<form>
<select name="alphabets" id="alphabets">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D" selected="selected">D</option>
</select>
</form>
</body>
</html>

When I pull the page the first time, option D is selected. If I select option A from the dropdown, and then do a page refresh, I want the dropdown option to go back to D. On page refresh, I want everything to go back to it's initial state. But the dropdown displays option A, or whatever was previously selected.

Any idea how can I make the page display option D on page refresh?

Thanks.

Share Improve this question asked Jul 10, 2012 at 22:49 user471317user471317 1,2312 gold badges22 silver badges35 bronze badges 2
  • 1 Are you refreshing in Firefox? It is the only browser I've ever worked in that kept form data on page refresh. stackoverflow./questions/1479233/… – TheZ Commented Jul 10, 2012 at 22:55
  • Have you tried the following in a document ready handler (or in a script block that appears after your select): $("#alphabets").val("D") – nnnnnn Commented Jul 10, 2012 at 22:56
Add a ment  | 

3 Answers 3

Reset to default 5

Place the following in a script tag on your page.

 $(function(){
      $('#alphabets option[value="D"]').attr('selected', true);
 });

This is a 'feature' with Firefox remembering selection on select lists.

Add autoplete="off" to the <select> tag.

$(document).ready(function() {
    $("#alphabets").val(localStorage.alphabets);
});

$("#alphabets").change(function() {
    localStorage.alphabets = $(this).val();
});

I think this will work (only in browsers that support localStorage) but I haven't tested it.

发布评论

评论列表(0)

  1. 暂无评论