在sessionStorage存储复选框值 [英] Store checkbox value in sessionStorage

查看:140
本文介绍了在sessionStorage存储复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以帮助我,我尝试使用 window.sessionStorage 来存储复选框的状态。当用户从第1页至第2页,然后单击后退按钮转到页次我希望所有才去第2页进行检查,用户选中的复选框。

Can any one help me I'm trying to store the state of checkboxes by using window.sessionStorage. When the user goes from page1 to page2 and then click back button to go to page1 I want all the checkboxes which the user checked before going to page2 to be checked.

这个环节有哪些工作,一旦它在localStorage的或sessionStorage模式
脚本在页面底部 http://elikirk.com/store-form-data-与-HTML5的localStorage的 - 和 - 的jQuery /

This link have the script at the bottom of the page which work once it is in the localStorage or sessionStorage mode
http://elikirk.com/store-form-data-with-html5-localstorage-and-jquery/.

推荐答案

谢谢大家。我已经通过我自己解决吧。

Thanks everyone. I have resolved it by myself.

   $('#id of the form').submit(function (ev) {
  sessionStorage.setItem('checkBoxesResult', $("#id of the form").serialize()); 
  });

if(sessionStorage.getItem("checkBoxesResult")) {

        var printVal = sessionStorage.getItem("checkBoxesResult");
        var checkBoxArray = [];
        if (printVal != null) {
        checkBoxArray = printVal.split("&");
        for( var i=0; i < checkBoxArray.length; i++ ) {

            var nv  = checkBoxArray[i].split("=")
            n = decodeURIComponent(nv[0]),
            v = nv.length > 1 ? decodeURIComponent(nv[1]) : null;

            selectedChkBox(n,v);
        }
    }
    }


   function selectedChkBox( chkbox, value ) {
      $("[name="+chkbox+"]").each( function() {   
     if ( $(this).val() ==  value )
        $(this).attr('checked', true);
     else
        if ( $(this).attr('checked') == true)
            $(this).attr('checked', false);
    });
     }

这篇关于在sessionStorage存储复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆