0

So I am using jQuery and have setup the jquery cookie plugin.

I have 4 drop down lists on my page, and I want to save the user's selections in a cookie, so when they come back to the page I automatically pre-select their previous selections.

I added a class to all my drop downs "ddl-cookie", and I was just thinking if I could somehow loop through all the drop down lists using the class, and save the selection and also loop to set the selections when the user returns to the page.

$(".ddl-cookie").each(function() {

});

It seems that given a cookie name, I can save a single key/value in the cookie.

So I'm guessing the only way for me to do this would be to have a comma separated list of drop down list names and values (selection value)?

4

1 に答える 1

1

You are correct. Cookies are intended to store a single piece of data, so the most common way to handle this is to serialize your data into an easy to retrieve format. That format is up to you, but you might use something like:

field_1=value1&field_2=value&...

You might want to also encode this data--remember that cookies are transferred as part of the request header. The pseudo code would go something like this:

// Store the data, using your own defined methods
data = serialize_data(data);
data = encode_data(data);
cookie = data;

// Retrieve the data using your own defined methods
data = cookie;
data = unencode_data(data)
data = deserialize_data(data)
于 2012-08-21T18:48:54.530 に答える