[英]Store checkbox value in sessionStorage
Can any one help me I'm trying to store the state of checkboxes by using window.sessionStorage
. 任何人都可以帮助我,我正在尝试使用
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. 当用户从第1页转到第2页,然后单击“后退”按钮转到第1页时,我希望选中用户在进入第2页之前选中的所有复选框。
This link have the script at the bottom of the page which work once it is in the localStorage or sessionStorage mode 此链接在页面底部具有脚本,该脚本一旦处于localStorage或sessionStorage模式即可工作
http://elikirk.com/store-form-data-with-html5-localstorage-and-jquery/ . 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 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... 如果没有,请发布您完整代码的JSFiddle(如果可能),以便我调试您的代码...
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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.