【问题标题】:How to store form values on localStorage?如何在 localStorage 上存储表单值?
【发布时间】:2014-09-13 15:57:32
【问题描述】:

我需要为大学工作制作一个 JqueryMobile 应用程序, 我有那个表格-

<form name="local_storage_form" method="post" action="#" data-ajax="false" autocomplete="on" data-  theme="e" style="padding: 10px;" onsubmit="save_data()">


     <label for="id">identity card number</label><input type="text" name="id" id="id" placeholder="identity card number..." data-theme="e" required class="localStore">
     <label for="hphone">Home Phone Number</label><input type="tel" name="hphone" id="hphone" placeholder="Home Phone Number..." data-theme="e" class="localStore">
     <label for="mphone">Mobile Phone Number</label><input type="text" name="mphone" id="mphone" placeholder="Mobile Phone Number..." data-theme="e" required class="localStore" oninvalid="setCustomValidity('Invaild Phone Number (05********)')">
     <label for="bdate">Birth Date</label><input type="date" name="bdate" placeholder="Birth Date..." id="bdate" data-theme="e" required class="localStore">
     <label for="email">Email Address</label><input type="email" name="email" id="email" autocomplete="off" placeholder="Email Address..." data-theme="e" required class="localStore">
     <button type="button" value="Save" id="Save" data-theme="a">Submit</button>
     </form>

我需要将所有详细信息存储在本地存储中并在之后获取它们, 当我点击提交按钮时,我该怎么做?

顺便说一句,这是一个离线网站,所以当我点击提交时,它会给我一个错误。

【问题讨论】:

    标签: html forms jquery-mobile submit local-storage


    【解决方案1】:

    查看Jquery API 文档。

    $('#local_storage_form').submit(function(e) {
        e.preventDefault(); // prevent the form from attempting to send to the web server
        var $inputs = $('#local_storage_form :input');
    
        var values = {};
        $inputs.each(function() {
            values[this.name] = $(this).val();
        });
    
        localStorage.setItem('testForm', JSON.stringify(values));
    });
    

    【讨论】:

    • 我做到了,但仍然没有得到它。我收到 HTTP 错误 405.0 - 单击提交按钮时不允许使用方法..
    • @OfirOhayon - 您需要阻止默认表单提交,以免将请求发送回 Web 服务器。
    • 抱歉我的无知,但我仍然无法保存这些值。我应该为表单提供什么方法​​/操作?我想让另一个页面仅供注册用户访问..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2020-05-30
    • 1970-01-01
    相关资源
    最近更新 更多