1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script type= "text/javascript" > function setCookie(name, value) { var exp = new Date(); exp.setTime(exp.getTime() + 24 * 60 * 60 * 1000 ); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } function getCookie(name) { var regExp = new RegExp( "(^| )" + name + "=([^;]*)(;|$)" ); var arr = document.cookie.match(regExp); if (arr == null ) { return null ; } return unescape(arr[ 2 ]); } </script> <select id= "select_1" onclick= "setCookie('select_1',this.selectedIndex)" > <option value= "apple" >apple</option> <option value= "banana" >banana</option> <option value= "cake" >cake</option> </select> <script type= "text/javascript" > var selectedIndex = getCookie( "select_1" ); if (selectedIndex != null ) { document.getElementById( "select_1" ).selectedIndex = selectedIndex; } </script> |
用JS把選擇值存入COOKIE,然後載入頁面的時候取出值即可。