使用Jquery在文本框中进行本地存储(Local storage in textboxes using Jquery)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
使用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

更多推荐

本文发布于:2023-04-13 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c826e843c0cc28bb7a19a6de7c556252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   文本   Jquery   textboxes   storage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!