我在创建并获取包含多个值的cookie时遇到了一些麻烦。 好吧,我想做的是,当单击提交按钮时,在其中创建一个带有表单输入值的cookie。然后,当同一用户重访网站时,表单将自动填充用户上次输入的内容。
我的HTML:
<form id="formen">
<fieldset>
<legend>Login</legend>
<label for="firstname" class="label">Firstname</label>
<input type="text" name="firstname" id="firstname" class="text" maxlength="30" />
</br></br>
<label for="lastname" class="label">Lastname</label>
<input type="text" name="lastname" id="lastname" class="text" maxlength="30" />
</br></br>
<label for="lastname" class="label">Address</label>
<input type="text" name="third" id="address" class="text" maxlength="30" />
</br></br>
<label for="lastname" class="label">City</label>
<input type="text" name="city" id="city" class="text" maxlength="30" data-sayt-exclude/>
</br></br>
<label for="lastname" class="label">Zipcode</label>
<input type="number" name="zipcode" id="zipcode" class="text" maxlength="30" />
</br></br>
<label for="lastname" class="label">E-mail</label>
<input type="email" name="email" id="email" class="text" maxlength="30" />
</br></br>
<label for="lastname" class="label">Phone</label>
<input type="number" name="phone" id="phone" class="text" maxlength="30" />
</br></br>
<input type="submit" name="submit" value="Remember Me" id="remember"/>
</fieldset>
</form>
我的javascript / jQuery:
$(document).ready(function () {
var date = new Date();
date.setTime(date.getTime() + (60 * 1000));
if ($("#remember").click(function () {
$.cookie('firstnameCookie', $firstnameVariable.val(), { expires: date });
$.cookie('lastnameCookie', $lastnameVariable.val(), { expires: date });
$.cookie('addressCookie', $addressVariable.val(), { expires: date });
$.cookie('cityCookie', $cityVariable.val(), { expires: date });
$.cookie('zipcodeCookie', $zipcodeVariable.val(), { expires: date });
$.cookie('emailCookie', $emailVariable.val(), { expires: date });
$.cookie('phoneCookie', $phoneVariable.val(), { expires: date });
}));
//set the value of the cookie to the element
var $firstnameVariable = $("#firstname").val($.cookie("firstnameCookie"));
var $lastnameVariable = $("#lastname").val($.cookie("lastnameCookie"));
var $addressVariable = $("#address").val($.cookie("addressCookie"));
var $cityVariable = $("#city").val($.cookie("cityCookie"));
var $zipcodeVariable = $("#zipcode").val($.cookie("zipcodeCookie"));
var $emailVariable = $("#email").val($.cookie("emailCookie"));
var $phoneVariable = $("#phone").val($.cookie("phoneCookie"));
});
这段代码实际上可以解决问题。它保存不同的输入值,并在用户重新访问站点时自动填充。
唯一的问题是我不想为每个输入值创建一个新的cookie。 是否可以只使用其中的每个输入值创建一个cookie,并让它自动填充不同的输入字段?
希望有人可以帮助我!无法弄清楚.. THX答案 0 :(得分:0)
在保存cookie之前将表单值连接成字符串,并在必须分配时将其拆分。 这些是我用来连接和拆分的函数:
var splitQueryString = function (q) {
var pars = q.split("&");
var qq = {};
var i = 0;
for (i = 0; i < pars.length; i++) {
var ret = pars[i].toString().split("=");
qq[ret[0]] = decodeURIComponent(ret[1]);
}
return qq;
};
var getQueryString = function (pars) {
var q = '';
var value;
var key;
for (key in pars) {
if (pars.hasOwnProperty(key)) {
value = pars[key];
if (!(value === null)) {
q += "&" + key + "=" + encodeURIComponent(value);
}
}
}
if (q.length > 0) {
//remove first
q = q.substr(1);
}
return q;
};
您的代码可能会变成:
if ($("#remember").click(function () {
var pars = {};
pars.firstnameVariable = $firstnameVariable.val();
pars.lastnameVariable = $lastnameVariable.val();
var cookie = getQueryString(pars);
$.cookie('formCookie', cookie, { expires: date });
}));
//set the value of the cookie to the element
var pars = splitQueryString($.cookie("formCookie"));
var $firstnameVariable = $("#firstname").val(pars.firstnameVariable);
var $lastnameVariable = $("#lastname").val(pars.lastnameVariable);
答案 1 :(得分:0)
通过将对象转换为字符串来存储数据
var obj = {
"firstname" : $("#firstname").val(),
"lastname" : $("#firstname").val()
};
$.cookie('data', JSON.stringify(obj), { expires: date });
当你读出来的时候:
var data = $.cookie("data"),
obj = data ? JSON.parse(data) : "";
Object.keys(obj).forEach(function(key){
$("#" + key).val(obj[key]);
});
我个人会使用localstorage而不是cookie。