Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:在输入字段上使用web存储/本地存储_Javascript_Jquery_Html_Local Storage_Web Storage - Fatal编程技术网

Javascript jQuery:在输入字段上使用web存储/本地存储

Javascript jQuery:在输入字段上使用web存储/本地存储,javascript,jquery,html,local-storage,web-storage,Javascript,Jquery,Html,Local Storage,Web Storage,我正在尝试保存/检索表单中所有输入字段的值。不幸的是,这不起作用。 令人沮丧的是,我不知道出了什么问题。我已经测试了类型:键和值都是字符串。事件已正确附加,将触发控制台.log。 你在这段代码中看到什么让你印象深刻的东西了吗?为什么不保存或检索值 (function ($) { if (typeof (window.localStorage) != "undefined") { $.each($("#myform input[type=text]"), function

我正在尝试保存/检索表单中所有输入字段的值。不幸的是,这不起作用。
令人沮丧的是,我不知道出了什么问题。我已经测试了类型:键和值都是字符串。事件已正确附加,将触发
控制台.log

你在这段代码中看到什么让你印象深刻的东西了吗?为什么不保存或检索值

(function ($) {
    if (typeof (window.localStorage) != "undefined") {
        $.each($("#myform input[type=text]"), function () {
            localStorage.getItem($(this).attr("id"));
        });
        $("#myform input[type=text]").live("change", function () {
            localStorage.setItem($(this).attr("id"), $(this).val());
        });
    }
})(jQuery);
当然,我正在一个支持web存储的浏览器中进行测试,并且每个输入都有一个id。

从jQuery 1.7开始,.live()方法已被弃用。使用.on()附加事件处理程序。较旧版本的jQuery用户应优先使用.delegate(),而不是.live()

(来自JQuery文档)

我猜你有两个选择:

$("#myform input[type=text]").on("change", function () {
        localStorage.setItem($(this).attr("id"), $(this).val());
 });

  • .live()
    在1.9中被删除,在1.7中被弃用
  • 在第一个
    循环中,我没有看到任何值设置器
    $(this).val(localStorage.getItem($(this).attr(“id”))
    ,用于填充值
    您使用的是哪个版本的jQuery?请将其放在JSFIDLE中。并显示html内容。@TusharGupta我使用的是jQuery 1.8.3版。看起来不错,谢谢,这解决了我的问题-我真是太蠢了!我检索了该值,但没有将其作为值输入!
    $("#myform input[type=text]").delegate("change", function () {
            localStorage.setItem($(this).attr("id"), $(this).val());
     });