使用Jquery在文本框中进行本地存储(Local storage in textboxes using Jquery)
我是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);但我无法做到这一点。
I am new in jquery and localstorage. I want to use localstorage to all textboxes using jquery.
<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 />I am trying to follow this script:
(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);But I couldn't make this work.
最满意答案
您需要重新设置值
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()); }); } });演示: 小提琴
You need to set the value back
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()); }); } });Demo: Fiddle
更多推荐
发布评论