刷新頁面時,要使下拉菜單(select)、raido保持不變。我想能通過cookies實現。刷新前先把select或radio的值保存在cookies中,刷新後再填回去。下面是測試代碼:
[html] view plain
- <select name="sex" id="sex" onchange="save()">
- <option value="01" selected >男</opton>
- <option value="02" >女</opton>
- </select>
-
- <input id="s1" type="radio" name="xueli" value="0" onclick="save()"/>本科
- <input id="s2" type="radio" name="xueli" value="1" checked onclick="save()"/>專科
[javascript] view plain
- <script language="javascript" type="text/javascript">
- function save() {
- selectIndex = document.getElementById("sex").selectedIndex;
- document.cookie = 'selectIndex =' + selectIndex;
- radios = document.getElementsByName("xueli");
- for (i = 0; i < radios.length; i++) {
- if (radios[i].checked) document.cookie = 'radioindex =' + i;
- }
- }
- window.onload = function () {
- var cooki = document.cookie;
- if (cooki != "") {
- cooki = "{\"" + cooki + "\"}";
- cooki = cooki.replace(/\s*/g, "").replace(/=/g, '":"').replace(/;/g, '","');
- var json = eval("(" + cooki + ")"); //將coolies轉成json對象
- document.getElementById("sex").options[json.selectIndex].selected = true;
- document.getElementsByName("xueli")[json.radioindex].checked = true;
- }
- else
- save();
- }
- </script>