我有一个通过 javascript 注入创建的网页,其中一个页面有一个下拉列表,如下所示:
html +="<select name='Sort' id='Sort' onchange='sortSwitch(document.getElementById(\"Sort\")[document.getElementById(\"Sort\").selectedIndex].value); display(document.getElementById(\"searchQuery\").value);return false;'>\n";
html +="<option></option>\n";
html +="<option value='4'>Smallest First</option>\n";
html +="<option value='5'>Largest First</option>\n";
html +="<option value='6'>A-Z</option>\n";
html +="<option value='7'>Z-A</option>\n";
html +="</select>";
下拉列表通过将所选选项传递给开关函数来过滤页面上显示的信息,另一个函数会重新加载此信息。但是,当 javascript 页面再次加载时,它会以空白选项开始。
有谁知道记住最后选择的排序的好方法?例如,如果我按“最小优先”排序,然后刷新页面,则该框将在空白处显示“最小优先”。我知道有一个“选项选择”属性,但我需要它是动态的。我觉得这是一件微不足道的事情,但我又无法具体地指出它。
提前致谢!
这是一篇关于JavaScript 中的 Cookie http://www.quirksmode.org/js/cookies.html其中包含其工作原理的说明,以及读取、写入和删除 cookie 的功能。
使用这些函数你会得到选定的值和写cookie像这样保存值:
var select = document.getElementById("Sort");
var selectedItem = select.options[select.selectedIndex].value;
createCookie("selectedItem",selectedItem);
then 读取cookie检索值并选择option
in the select
盒子,像这样:
var selectedItem = readCookie("selectedItem");
var select = document.getElementById("Sort");
select.value = selectedItem;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)