jQuery-从选择菜单设置和检索cookie
我有一个选择菜单,可以让您选择您喜欢的交货国家。当您选择一个选项时,它会在下面显示相关的国家/地区 我正在尝试使用设置cookie,以便用户始终记住最后的选择。以下是迄今为止我所拥有的,但它不起作用: 我想将select选项的值存储为cookie,然后使用相同的值显示相应的国家/地区信息。这是当前存储的cookie,看起来不正确:jQuery-从选择菜单设置和检索cookie,jquery,select,cookies,js-cookie,Jquery,Select,Cookies,Js Cookie,我有一个选择菜单,可以让您选择您喜欢的交货国家。当您选择一个选项时,它会在下面显示相关的国家/地区 我正在尝试使用设置cookie,以便用户始终记住最后的选择。以下是迄今为止我所拥有的,但它不起作用: 我想将select选项的值存储为cookie,然后使用相同的值显示相应的国家/地区信息。这是当前存储的cookie,看起来不正确: {%220%22:{%22jquery11205649891521717818%22:9}%2C%22length%22:1%2C%22context%22:{%2
{%220%22:{%22jquery11205649891521717818%22:9}%2C%22length%22:1%2C%22context%22:{%22location%22:{%22href%22:%22https://zed-labz.myshopify.com/pages/delivery%22%2C%22ancestorOrigins%22:{}%2C%22来源%22:%22https://zed-labz.myshopify.com%22%2C%22protocol%22:%22https:%22%2C%22host%22:%22zed-labz.myshopfify.com%22%2C%22主机名%22:%22zed labz.myshopfify.com%22%2C%22端口%22:%22%22%2C%22路径名%22:%22/pages/delivery%22%2C%22搜索%22:%22%22%2C%22哈希%22:%22%22}%2C%22mc嵌入订阅表单%22:{%220%22:{}%2C%221%22:{}}%2C%22选择器%22:%22#deliverycountry1%22}
一些问题:
- 保存jQuery集合,因为$countrycode不是该值:
var $countrycode = $('#delivery' + $(this).val());
- 从cookie中获取值来设置选择的时刻从未出现过
$(function () {
$('#country').change(function() {
$('.box').hide();
if( Cookies ) {
Cookies.set('deliveryOption', $(this).val());
}
$('#delivery' + $(this).val()).show().siblings().hide();
});
// On page load, read out the cookie, and select the corresponding country
// If no cookie, take country1 as default
var country = Cookies && Cookies.get('deliveryOption') || 'country1';
$('#country').val(country).trigger('change');
})
从中看到它
$(function () {
$('#country').change(function() {
$('.box').hide();
if( Cookies ) {
Cookies.set('deliveryOption', $(this).val());
}
$('#delivery' + $(this).val()).show().siblings().hide();
});
// On page load, read out the cookie, and select the corresponding country
// If no cookie, take country1 as default
var country = Cookies && Cookies.get('deliveryOption') || 'country1';
$('#country').val(country).trigger('change');
})