Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>localStorage can store only string values.</p> <p>first of all, you have to serialize the data use <a href="http://api.jquery.com/serialize/" rel="nofollow">jQuery.serialize</a>, then you can store it like </p> <p><code>localStorage['data']=$('form').serialize()</code></p> <p><strong>edit</strong></p> <p>For restore, you have make some rules.Add <code>data-binding</code> to tag, save form value as key/value pair, the key is <code>data-binding</code>, then you can restore it.</p> <p>Something like this</p> <pre><code>var data = {}; $('[data-binding]') .each(function(){ data[$(this).data('binding')] = $(this).serialize(); }) localStorage['data'] = JSON.stringify(data); </code></pre> <p>For restore</p> <pre><code>var data = JSON.parse(localStorage['data']); $('[data-binding]') .each(function() { // handle the set value // need consider the different value type for different field type var $this = $(this); var val = data[$this.data('binding')]; // for chechbox if($this.is('[type=checkbox]')) $this.prop('checked',val) // for others else $this.val(val); }) </code></pre> <p>Example in my code:</p> <pre><code>// u5 = utils U5 = {}; /** * 加载配置到界面,data-setting属性指定setting项 * @param setting */ U5['LoadSettingToUI'] = function (setting) { $('[data-setting]') .each(function() { var $this = $(this); var val; val = setting[$this.data('setting')]; // 根据不同的类型进行值的转换 for jquery mobile if($this.is('select[data-role=slider]'))// 这里没有使用jqmData来判断,考虑兼容问题 { $this.val(val?'on':'off'); }else if($this.is('[type=checkbox]')) { $this.prop('checked',!!val); }else{ $this.val(val); } }); }; /** * 从页面获取配置,data-setting属性指定setting项 * @return {object} */ U5['GetSettingFromUI'] = function () { var setting = {}; $('[data-setting]').each(function() { var $this = $(this); /** * @type {string|boolean} */ var val; val = $this.val(); // 根据不同的类型进行值的转换 if(/^on|off$/.test(val)) { val = val === 'on'; }else if($this.is('[type=checkbox]')) { val = $this.prop('checked'); } // setting[$this.data('setting')] = val; }); return setting; }; </code></pre> <p><strong>Full demo <a href="http://jsfiddle.net/Wener/X5zpe/" rel="nofollow">here</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload