How to display “selected radio button” after refresh?

谁说胖子不能爱 提交于 2020-01-02 12:01:32

问题


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>

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4954265/how-to-display-selected-radio-button-after-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!