admin管理员组

文章数量:1356905

After refresh the page, the user selected radio button is missing. How can I always display the user's "selected radio button" even I had refresh the page?

<input name="radiobutton" type="radio" value="" id="all" checked />

radio with checked is not suitable, because it will always display the default radio button.

Below is my code:-

<form name="myfrm" id="myfrm" action="" method="post">
    <input name="radiobutton" type="radio" value="" id="all" />
    <label>All Languages</label>
    <input name="radiobutton" type="radio" value="" id="english" />
    <label>English</label>
</form>

After refresh the page, the user selected radio button is missing. How can I always display the user's "selected radio button" even I had refresh the page?

<input name="radiobutton" type="radio" value="" id="all" checked />

radio with checked is not suitable, because it will always display the default radio button.

Below is my code:-

<form name="myfrm" id="myfrm" action="" method="post">
    <input name="radiobutton" type="radio" value="" id="all" />
    <label>All Languages</label>
    <input name="radiobutton" type="radio" value="" id="english" />
    <label>English</label>
</form>
Share Improve this question edited Sep 19, 2020 at 7:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 10, 2011 at 6:58 Always21Always21 651 gold badge2 silver badges7 bronze badges 2
  • You want to maintain the selected radio button on refresh or on postback bcoz refresh is just like u send a request to a page so it will display by default selected radio button that u set. – Sukhjeevan Commented Feb 10, 2011 at 7:08
  • possible duplicate of saving checkbox state on reload – user57508 Commented Feb 10, 2011 at 7:11
Add a ment  | 

3 Answers 3

Reset to default 3

I don't know if this is something you'd want. But a thing that es up to mind is the HTML5 Web Storage functionality.With that feature you can store data on the puter of the user.

So whenever a user changes an input field you can create a javascript call that stores the value into the localstorage:

localStorage.setItem(“inputName”, “value”);

Then when you load the page, you see if any of these values are stored and then fill them in.

You should conform to standards and give a value to every attribute. That means you need checked="checked"

<input name="radiobutton" type="radio" value="" id="all" checked="checked" />

And in case you were asking how to stick to what was selected previously: AFAIK, it is not possible with plain HTML. You need to bind a Javascript event that submits the selected radio button to your server and a dynamically generated page (by PHP, Python, whatever) that puts the 'checked="checked" to the radio button that was submitted as checked.

EDIT:

As I see it, submitting the currently selected radio button to the server and generating your page dynamically might be overkill.

i think this discussion is going to help you...though this discussion is about saving state of checkbox, but you can modify that code according to your need..hope this helps to you!!!

Save state of inputElement

本文标签: javascriptHow to display quotselected radio buttonquot after refreshStack Overflow