简体   繁体   中英

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. 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.

$(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/

Your code doesn't work as you don't assign the email_address variable anywhere. You should change that value to use $('#email_address').val() .

Also note that your jsFiddle isn't setup correctly; your URL to the external cookie script goes to a Github page. You need to use a CDN link instead. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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