开发者

How to display "selected radio button" after refresh?

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>


I don't know if this is something you'd want. But a thing that comes up to mind is the HTML5 Web Storage functionality.With that feature you can store data on the computer 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜