简体   繁体   中英

Store checkbox value in sessionStorage

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.

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

Ok, maybe its the way you using the sessionstorage...

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

Here is a link where localstorage and sessionstorage are explained

If not, please post a JSFiddle of your complete code (if possible), so I can debug your code...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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