最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retain radio input state on page refresh - Stack Overflow

programmeradmin6浏览0评论

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!

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!

Share Improve this question asked Sep 23, 2013 at 13:00 WasiimWasiim 1464 silver badges17 bronze badges 4
  • You'll need to use cookies or HTML 5 local storage, or have the server generate new HTML based on the POST data. Lots of options, pretty mon problem. What's your server-side technology? – Yuck Commented Sep 23, 2013 at 13:02
  • using any kind of persistent data client side as cookie or localStorage – A. Wolff Commented Sep 23, 2013 at 13:02
  • Look here, I think this old post can help you stackoverflow./questions/16206322/… – BENARD Patrick Commented Sep 23, 2013 at 13:02
  • before submit you can perform validations – Jagz S Commented Sep 23, 2013 at 13:03
Add a ment  | 

3 Answers 3

Reset to default 5

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

Maybe you store the information in the session token or in a session cookie. If you use html5 ponents, you might try to use html local storage.

you just need to update the code

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

It will save the property of radio button as user has selected for the radio button.

发布评论

评论列表(0)

  1. 暂无评论