zoukankan      html  css  js  c++  java
  • 刷新页面时 select值保持不变

    刷新页面时,要使下拉菜单(select)、raido保持不变,用ajax是无法实现的。我想只能通过cookies才能实现。刷新前先把select或radio的值保存在cookies中,刷新后再填回去。下面是测试代码:

    [html] view plain
    1. <select name="sex" id="sex" onchange="save()">  
    2.     <option  value="01" selected >男</opton>  
    3.     <option  value="02" >女</opton>  
    4. </select>  
    5.   
    6. <input id="s1" type="radio" name="xueli" value="0" onclick="save()"/>本科  
    7. <input id="s2" type="radio" name="xueli" value="1" checked onclick="save()"/>专科  
    [javascript] view plain
    1. <script language="javascript" type="text/javascript">  
    2.     function save() {  
    3.         selectIndex = document.getElementById("sex").selectedIndex;  
    4.         document.cookie = 'selectIndex =' + selectIndex;  
    5.         radios = document.getElementsByName("xueli");  
    6.         for (i = 0; i < radios.length; i++) {  
    7.             if (radios[i].checked) document.cookie = 'radioindex =' + i;  
    8.         }  
    9.     }  
    10.     window.onload = function () {  
    11.         var cooki = document.cookie;  
    12.         if (cooki != "") {  
    13.             cooki = "{"" + cooki + ""}";  
    14.             cooki = cooki.replace(/s*/g, "").replace(/=/g, '":"').replace(/;/g, '","');  
    15.             var json = eval("(" + cooki + ")"); //将coolies转成json对象  
    16.             document.getElementById("sex").options[json.selectIndex].selected = true;  
    17.             document.getElementsByName("xueli")[json.radioindex].checked = true;  
    18.         }  
    19.         else  
    20.             save();  
    21.     }  
    22. </script>  

    转自http://blog.csdn.net/fjnu2008/article/details/7519531

    您的资助是我最大的动力!
    金额随意,欢迎来赏!
    付款后有任何问题请给我留言。

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【火锅商人】!

  • 相关阅读:
    HDU 5640 King's Cake
    HDU 5615 Jam's math problem
    HDU 5610 Baby Ming and Weight lifting
    WHU1604 Play Apple 简单博弈
    HDU 1551 Cable master 二分
    CodeForces659C Tanya and Toys map
    Codeforces 960E 树dp
    gym 101485E 二分匹配
    Codeforces 961E 树状数组,思维
    Codeforces Round #473 (Div. 2) D 数学,贪心 F 线性基,模板
  • 原文地址:https://www.cnblogs.com/swads/p/5313936.html
Copyright © 2011-2022 走看看