在 cookie 中保存下拉菜单选择? [英] Saving dropdown menu selection in a cookie?
问题描述
我已经多次看到这个帖子,但我根本无法让代码运行.我需要帮助让这个下拉菜单将其设置保存到一个 cookie,所以当用户再次访问该站点时,他们之前的选择会被保留.
I've seen this posted a few times, but I wasn't able to get the code working at all. I need help making this drop down menu save its settings to a cookie, so when the user visits the site again, their previous selection is retained.
下拉列表:
<select id="ThemeSelect">
<option value="zelda">Zelda</option>
<option value="smb2">SMB 2</option>
</select>
作为参考,此代码与 Wordpress 小部件中的一些 Javascript 一起更改了 css 代码,例如主题选择器.
For reference, this code is with some Javascript in a Wordpress widget that changes the css code, like a theme selector.
我如何将它保存为 cookie?我觉得我什么都试过了!
How would I make this save as a cookie? I feel like I've tried everything!
我应该说,但我使用此代码来更改 CSS:
I should have said, but I use this code to change the CSS:
var saveclass = null;
var sel = document.getElementById('ThemeSelect');
sel.onchange = function(){
saveclass = saveclass ? saveclass : document.body.className;
document.body.className = saveclass + ' ' + sel.value;
};
推荐答案
这些方面的东西应该适合你.通过javascript创建cookie的函数在从 JavaScript 设置 Cookie,javascripter.net 上的帖子.
Something along these lines should work for you. The function to create a cookie via javascript was found in Setting a Cookie from JavaScript, a post on javascripter.net.
HTML:
<select id="ThemeSelect" onchange="setCookie('theme', this.value, 365);">
<option value="zelda">Zelda</option>
<option value="smb2">SMB 2</option>
</select>
Javascript:
function setCookie(cookieName, cookieValue, nDays) {
var today = new Date();
var expire = new Date();
if (!nDays)
nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
编辑:
保存 cookie
我已经为你将这两个功能合二为一了.
I have merged the two functions into one for you.
HTML:
<select id="ThemeSelect" onchange="saveTheme(this.value);">
<option value="zelda">Zelda</option>
<option value="smb2">SMB 2</option>
</select>
Javascript:
var saveclass = null;
function saveTheme(cookieValue)
{
var sel = document.getElementById('ThemeSelect');
saveclass = saveclass ? saveclass : document.body.className;
document.body.className = saveclass + ' ' + sel.value;
setCookie('theme', 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) + ";expires="+expire.toGMTString();
}
返回页面时读取 cookie
感谢 dunsmoreb
Thanks to dunsmoreb
我们可以使用这个函数获取cookie,无耻地从这个问题中窃取.
We can get the cookie using this function, shamelessly stolen from this question.
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
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;
}
然后我们需要选择页面加载时的值.以下代码将完成此任务:
Then we need to select the value when the page loads. The following code will accomplish just that:
document.addEventListener('DOMContentLoaded', function() {
var themeSelect = document.getElementById('ThemeSelect');
var selectedTheme = readCookie('theme');
themeSelect.value = selectedTheme;
saveclass = saveclass ? saveclass : document.body.className;
document.body.className = saveclass + ' ' + selectedTheme;
});
这篇关于在 cookie 中保存下拉菜单选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!