2014-09-13 67 views
1

我需要爲學院工作, 一個JqueryMobile應用程序,我有一個形式 -如何在localStorage上存儲表單值?

<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> 

我需要存儲在localStorage的所有細節,之後讓他們,我 怎麼能做到這一點,當我點擊提交按鈕?

順便說一句,這是一個離線網站,所以當我點擊提交它給了我一個錯誤。

回答

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)); 
}); 
+0

我做了,但還是沒有得到它。 我得到一個HTTP錯誤405.0-方法不允許當我點擊提交按鈕.. – 2014-09-13 16:12:51

+0

http://www.checkupdown.com/status/E405.html – Corvusoft 2014-09-13 16:14:00

+0

@OfirOhayon - 你需要防止默認的表單提交,讓你'不會將請求發送回Web服務器。 – tvanfosson 2014-09-13 16:19:16