如何在页面刷新时保留无线电输入状态 [英] How to retain radio input state on page refresh

查看:64
本文介绍了如何在页面刷新时保留无线电输入状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,如果用户提交有错误的表单(如果页面会刷新),我需要存储所有单选按钮的状态。



<我希望实现与此类似的东西:

  $(function(){
$('。示例输入[type =radio]:选中')。each(function(){
$(this).attr(checked,true);
});

非常感谢任何帮助!

解决方案

您可以将其状态存储在本地存储中,并在每次加载页面后将其恢复。



存储值的小例子:

  $('#myCheckbox')。click(function(){
localStorage ['my.checkbox'] = this.checked;
});

恢复它:

  $('#myCheckbox')。prop('checked',localStorage ['my.checkbox'] =='true'); 


i have a form on which i need to store the state of all radio buttons if a user submit the form with errors (which in case the page would refresh).

I want to achieve something quite similar to this:

$(function(){
        $('.example input[type="radio"]:checked').each(function(){
        $(this).attr("checked",true);
    });

Any help would much be appreciated!

解决方案

You could store it's state within local storage and restore it after each page load.

Little example on storing the value:

$('#myCheckbox').click(function () {
    localStorage['my.checkbox'] = this.checked;
});

Restore it:

$('#myCheckbox').prop('checked', localStorage['my.checkbox'] == 'true');

这篇关于如何在页面刷新时保留无线电输入状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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