简体   繁体   English

如何使用Jquery JS Cookie检索Cookie值

[英]How to Retrieve Cookie Value using Jquery JS Cookie

I am using a jQuery cookie plugin called JS Cookie and what happens is that I am unable to retrieve the data or the value in the cookie. 我正在使用一个名为JS Cookie的jQuery cookie插件,结果是我无法检索cookie中的数据或值。 I would like to store the value of the cookie that was inputted on the text box and retrieve it in the text field when the page is visited again. 我想存储在文本框中输入的cookie的值,并在再次访问页面时在文本字段中检索它。

$(document).ready(function() {
    var cookie_email = Cookies.get('user_email');
    $('#email_address').val(cookie_email);

    $('#test_button').click(function() {
        Cookies.set('user_email', email_address, {
            expires: 365
        });
    });
});

https://jsfiddle.net/mue1amcm/ https://jsfiddle.net/mue1amcm/

Your code doesn't work as you don't assign the email_address variable anywhere. 您的代码不起作用,因为您没有在任何地方分配email_address变量。 You should change that value to use $('#email_address').val() . 您应该更改该值以使用$('#email_address').val()

Also note that your jsFiddle isn't setup correctly; 另请注意,您的jsFiddle设置不正确; your URL to the external cookie script goes to a Github page. 您的外部cookie脚本的URL转到Github页面。 You need to use a CDN link instead. 您需要使用CDN链接。 Try this: 试试这个:

var cookie_email = Cookies.get('user_email');
$('#email_address').val(cookie_email);

$('#test_button').click(function() {
    Cookies.set('user_email', $('#email_address').val(), {
        expires: 365
    });
});

Working example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM