java 刷新页面或提交请求后保持选中单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10303349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:28:18  来源:igfitidea点击:

Keep the radio button selected after refreshing the page or submitting the request

javahtmljspstrutsdom-events

提问by Susie

I understand this question may sound very basic but I have been trying to solve it and I still haven't been able to do it. I will really appreciate any help.

我知道这个问题可能听起来很基本,但我一直在尝试解决它,但仍然无法解决。我真的很感激任何帮助。

I have two radio buttons in my jsp page. When either one is selected, it calls a Javascript function. What eventually happens is if you select radio button 1, it displays data from table 1, if radio button 2 is selected, then data from table 2 is displayed.

我的 jsp 页面中有两个单选按钮。选择任一一个时,它会调用JavaScript函数。最终会发生的是,如果您选择单选按钮 1,它将显示表 1 中的数据,如果选择单选按钮 2,则显示表 2 中的数据。

As a result, my page gets refreshed and the radio buttons are not selected anymore. I would like to show the user which radio button was selected.

结果,我的页面被刷新并且不再选择单选按钮。我想向用户显示选择了哪个单选按钮。

function viewUploadedData(){
    document.location.href = "ForecastInquiryFilter.do?exportVar=viewUploadedData";

function viewTopsData(radio){
    document.location.href = "ForecastInquiryFilter.do?exportVar=viewTopsData";
}
<input type="radio" name="dataTypeToView" id="topsData" onclick="viewTopsData();"/>
<label for="topsData">TOPS Data</label><br/>
<input type="radio" name="dataTypeToView" id="uploadedData" onclick="viewUploadedData();"/>
<label for="uploadedData">Uploaded Data </label>

回答by Howard Schutzman

If you generate the html for a radio button using the "checked" attribute, it will be checked. For example, the following html will generate a radio button that is checked:

如果您使用“已检查”属性为单选按钮生成 html,它将被检查。例如,以下 html 将生成一个选中的单选按钮:

<input type=radio name=myradio value=1 checked>

So, having said that, you need to modify your jsp to conditionally generate the "checked" attribute for the appropriate radio button. Presumably your jsp knows which radio button was checked, so hopefully this should be easy to code.

因此,话虽如此,您需要修改您的 jsp 以有条件地为相应的单选按钮生成“已检查”属性。大概你的jsp知道哪个单选按钮被选中,所以希望这应该很容易编码。

So, you jsp code might look something like this:

因此,您的 jsp 代码可能如下所示:

<%
String whichRadio = request.getParameter("myradio");
String r1checked = "";
if (whichRadio.equals("1")) r1checked = " checked";
String r2checked = "";
if (whichRadio.equals("2")) r2checked = " checked";
%>
<input type=radio name=myradio value="1"<%= r1Checked %>>
<input type=radio name=myradio value="2"<%= r2Checked %>>

The above code will generate the checked attribute for whichever radio was checked when your jsp was called. If no radio was checked, then no checked attribute will be generated.

上面的代码将为调用您的jsp 时检查的任何无线电生成checked 属性。如果没有选中radio,则不会生成checked 属性。

回答by Diodeus - James MacFarlane

You need to set the CHECKED property of the radio button ON THE SERVER before re-sending the page back to the browser.

在将页面重新发送回浏览器之前,您需要在服务器上设置单选按钮的 CHECKED 属性。

HTTP is a stateless protocol.

HTTP 是一种无状态协议

回答by user2418306

Using jstl

使用 jstl

<input type="radio" name="digest" value="md5" <c:if test="${digest == 'md5' or empty digest}">checked</c:if>>md5
<input type="radio" name="digest" value="sha1" <c:if test="${digest == 'sha1'}">checked</c:if>>sha1