Quantcast
Channel: iT邦幫忙
Viewing all articles
Browse latest Browse all 15645

請問sessionStorage怎麼利用變數做區隔?

$
0
0
各位好,我想我下的這個題目很模糊,應該不知道我想要表達什麼吧!
我用例子來說明我的問題
以下是一個可以運行的code
<script type="text/javascript">
        $(function () {
            $("#date_1").datepicker({
                dateFormat: 'yy/mm/dd'
            });
            $("#date_2").datepicker({
                dateFormat: 'yy/mm/dd'
            });
	    $("#date_3").datepicker({
                dateFormat: 'yy/mm/dd'
            });
        });</script><script type="text/javascript">
        window.onunload = function saveSession() {
            var date1 = document.getElementById("date_1");
            var date2 = document.getElementById("date_2");
            var date3 = document.getElementById("date_3");
            var mydata1 = date1.value;
            var mydata2 = date2.value;
            var mydata3 = date3.value;
            var oStorage1 = window.sessionStorage;
            var oStorage2 = window.sessionStorage;
            var oStorage3 = window.sessionStorage;
            oStorage1.mydata = mydata1;
            oStorage2.mydata = mydata2;
            oStorage3.mydata = mydata3;
        }
        function readSession() {
            var date1 = document.getElementById("date_1");
            var date2 = document.getElementById("date_2");
            var date3 = document.getElementById("date_3");
            var oStorage1 = window.sessionStorage;
            var oStorage2 = window.sessionStorage;
            var oStorage3 = window.sessionStorage;
            var mydata1 = "請選擇日期";
            var mydata2 = "請選擇日期";
            var mydata3 = "請選擇日期";
            if (oStorage1.mydata) {
                mydata1 = oStorage1.mydata;
            }
            if (oStorage2.mydata) {
                mydata2 = oStorage2.mydata;
            }
            if (oStorage3.mydata) {
                mydata3 = oStorage3.mydata;
            }
            date1.value = mydata1;
            date2.value = mydata2;
            date3.value = mydata3;
        }</script>

<input name="AA" type="text" id="date_1" />
<input name="BB" type="text" id="date_2" />
<input name="CC" type="text" id="date_3" />
但這段程式的缺點是一定要3個input都填了才能做保留的動作,不然只要按F5就全部都消失
我想要問能否可以只填一個其它不要動,值一樣做保留...

要麻煩大家了,感謝各位~

Viewing all articles
Browse latest Browse all 15645

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>