/**
* @desc 将数据写入cookie,并返回对象值。
* @param obj 文本框id 数组例如['aaa', 'bbb', 'ccc'] <input type="text" id="aaa" />
* @param loadType 是否是页面加载 true 是。false 输入调用
*/
function setInputVal(obj, loadType) {
let _url = location.href;
let obje = {};
for (let item of obj) {
let itemVal = $('#' + item).val();
// 优先使用cookie中的值
if (loadType === true) {
let cookieVal = getCookieVal(item);
if (!itemVal && cookieVal) {
itemVal = cookieVal;
}
}
// 将获取到的信息输入到当前页的cookie中
document.cookie = item + '=' + itemVal + '; path=' + _url;
// 给输入框赋值
$('#' + item).val(itemVal);
// 返回文本框中对应的值
obje[item] = itemVal;
}
// 将获取的输入值返回
return obje;
}
JS 将获取的信息写入cookie
于 2023-05-06 14:54:59 首次发布