繁体   English   中英

将复选框值存储在sessionStorage中

[英]Store checkbox value in sessionStorage

任何人都可以帮助我,我正在尝试使用window.sessionStorage存储复选框的状态。 当用户从第1页转到第2页,然后单击“后退”按钮转到第1页时,我希望选中用户在进入第2页之前选中的所有复选框。

此链接在页面底部具有脚本,该脚本一旦处于localStorage或sessionStorage模式即可工作
http://elikirk.com/store-form-data-with-html5-localstorage-and-jquery/

好的,也许这是您使用会话存储的方式...

http://www.w3schools.com/html/html5_webstorage.asp

这是解释本地存储和会话存储的链接

如果没有,请发布您完整代码的JSFiddle(如果可能),以便我调试您的代码...

感谢大家。 我自己解决了。

   $('#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);
    });
     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM