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