我是 jquery 和 localstorage 的新手。我想使用 jquery 对所有文本框使用 localstorage。
<input id="in-1" type="text" />
<br />
<input id="in-2" type="text" />
<br />
<input id="in-3" type="text" />
<br />
<input id="in-4" type="text" />
<br />
我正在尝试遵循这个脚本:
(function ($) {
if (typeof (window.localStorage) != "undefined") {
$.each($("input[type=text]"), function () {
localStorage.getItem($(this).attr("id")));
});
$("input[type=text]").on("change", function () {
localStorage.setItem($(this).attr("id"), $(this).val());
});
}
})(jQuery);
但我无法完成这项工作。
最佳答案
您需要重新设置该值
jQuery(function ($) {
if (typeof (window.localStorage) != "undefined") {
//set the value to the text fields
$("input[type=text]").val(function () {
return localStorage.getItem(this.id);
});
$("input[type=text]").on("change", function () {
localStorage.setItem(this.id, $(this).val());
});
}
});
演示:Fiddle
关于jquery - 使用 Jquery 在文本框中本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324035/