加载后无法从cookie中设置<select>值(location.href +&#34; #chatContainer&#34;);

时间:2017-03-13 14:44:13

标签: javascript jquery ajax

我正在我的网页上进行简单的聊天。我想将选定的用户(值)保存到cookie,以便在重新加载后保持用户选择。 我的代码: &lt; div id =&#34; chatContainer&#34;名称=&#34; chatCointainer&#34;&GT; &lt; select onChange =&#34; clearAndRefresh()&#34; ID =&#34; selectUser&#34;命名=&#34; selectUser&#34;大小=&#34; 24&#34;&GT;     &lt;选择选项=&#34;选择&#34;值=&#34; 1&#34;&GT;用户1&LT; /选项&GT;     &lt; option value =&#34; 2&#34;&gt; User2&lt; / option&gt;     &lt; option value =&#34; 3&#34;&gt; User3&lt; / option&gt;     &lt; option value =&#34; 4&#34;&gt; User4&lt; / option&gt; &LT; /选择&GT; &LT; / DIV&GT; &lt; script type =&#34; text / javascript&#34;&gt;     window.setInterval(refreshOnly,5000);     var saveclass = null;     function saveUserSelection(){         var sel = document.getElementById(&#39; selectUser&#39;);         var cookieValue = document.getElementById(&#39; selectUser&#39;)。value;         saveclass = saveclass? saveclass:document.body.className;         document.body.className = saveclass +&#39; &#39; + sel.value;         setCookie(&#39; userselection&#39;,cookieValue,365);     }     function setCookie(cookieName,cookieValue,nDays){         var today = new Date();         var expire = new Date();         if(nDays == null || nDays == 0)             nDays = 1;         expire.setTime(today.getTime()+ 3600000 * 24 * nDays);         document.cookie = cookieName +&#34; =&#34; + escape(cookieValue)+&#34 ;; expires =&#34; + expire.toGMTString();     }     function readCookie(name){         var nameEQ = name +&#34; =&#34 ;;         var ca = document.cookie.split(&#39 ;;&#39;);         for(var i = 0; i&lt; ca.length; i ++){             var c = ca [i];             while(c.charAt(0)==&#39;&#39;)c = c.substring(1,c.length);             if(c.indexOf(nameEQ)== 0)return c.substring(nameEQ.length,c.length);         }         return null;     }     function getUserSelectionFromCookie(){         var userSelect = document.getElementById(&#39; selectUser&#39;);         var selectedUser = readCookie(&#39; userselection&#39;);         userSelect.value = selectedUser;         saveclass = saveclass? saveclass:document.body.className;         document.body.className = saveclass +&#39; &#39; + selectedUser;     }     function showUnreadMessages(){         $ .ajaxSetup({cache:false});         $(&#34;#chatContainer&#34;)。load(location.href +&#34;#chatContainer&#34;);     }     function clearAndRefresh(){         saveUserSelection();         $(&#34;#userMessage&#34)。VAL(&#34;&#34);     }     function refreshOnly(){         saveUserSelection();         showUnreadMessages();         getUserSelectionFromCookie();     } &LT; /脚本&GT; 我的问题是之后 $ .ajaxSetup({cache:false}); $(&#34;#chatContainer&#34;)。load(location.href +&#34;#chatContainer&#34;); 没有选定的值。 请帮忙,问候。

0 个答案:

没有答案