javascript 刷新后如何显示“选定的单选按钮”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4954265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display "selected radio button" after refresh?
提问by Always21
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>
采纳答案by riha
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.
如果您问如何坚持之前选择的内容:AFAIK,纯 HTML 是不可能的。您需要绑定一个 Javascript 事件,该事件将选定的单选按钮提交到您的服务器,以及一个动态生成的页面(通过 PHP、Python 等)将“checked="checked”放到提交为已检查的单选按钮上。
EDIT:
编辑:
As I see it, submitting the currently selected radio button to the server and generating your page dynamically might be overkill.
在我看来,将当前选择的单选按钮提交到服务器并动态生成页面可能有点矫枉过正。
回答by Timo Willemsen
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.
我不知道这是不是你想要的。但想到的是 HTML5 网络存储功能。借助该功能,您可以将数据存储在用户的计算机上。
So whenever a user changes an input field you can create a javascript call that stores the value into the localstorage:
因此,每当用户更改输入字段时,您都可以创建一个 javascript 调用,将值存储到 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.
然后,当您加载页面时,您会查看是否存储了这些值中的任何一个,然后填写它们。
回答by Vivek
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!!!
我认为这个讨论会帮助你...虽然这个讨论是关于保存复选框的状态,但你可以根据你的需要修改该代码..希望这对你有帮助!!!

