【问题标题】:How can you show a radio button selection on a refreshed page如何在刷新的页面上显示单选按钮选择
【发布时间】:2021-07-27 18:39:41
【问题描述】:

我是编码新手,如果我的英语不好,我深表歉意。我有一组单选按钮,当我单击它时,它会显示我的表格,但会针对选择进行过滤。因此,如果从 3 中选择数学,则仅显示具有数学的记录,然后我可以导出或打印此数据。但问题是,当我选择数学时,它会取消选中收音机,因此无法按下导出按钮,因为没有选择按钮。

有没有办法让按钮保持选中状态?

我尝试使用localStorage.setItem("radio", value);,但这也没有用

我的 html 和脚本:

<div>
<input type="radio" name="download" id="1" value="/mathsrecord"/>Math <br />
<input type="radio" name="download" id="2" value="/englishrecord"/>English <br />
<input type="radio" name="download" id="2" value="/accountingrecord"/>Acc <br />
<input type="radio" name="download" id="3" value="/all"/>All <br />
        </div>
<button type="button" class="btn btn-primary" id="download" value="download">Export to Excel</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
                <script>
                $('input[type="radio"]').on('click', function() {
                     window.location = $(this).val();
                     localStorage.setItem("radio", value);
                });
                </script>
<script>
var radio_1 = document.getElementById('1');
var radio_2 = document.getElementById('2');
var radio_3 = document.getElementById('3');
var button = document.getElementById('download');

button.onclick = downloadFile;

function downloadFile() {
    if(radio_1.checked) {
        window.open("/exportmath/excel");
    }else if(radio_2.checked) {
        window.open("/exporteng/excel");
    } else if(radio_3.checked) {
      window.open("/exportacc/excel");
    }
}</script>

【问题讨论】:

  • “我尝试使用 localStorage.setItem("radio", value);但这也不起作用” - 你期望它会做什么?单独存储值不会影响单选按钮的行为方式。您仍然需要实现第二部分 - 在页面加载时从本地存储读取此值,然后设置相应的单选按钮进行检查。
  • 你能给我链接一篇文章吗?
  • stackoverflow.com/a/29755453/1427878 有一个例子;如果你需要更多,那么请先自己做一些研究。

标签: javascript php html


【解决方案1】:

您需要在重新加载页面后将值设置回单选按钮。

这样

var itemValue = localStorage.getItem("radio");
if (itemValue !== null) {
   $("input[value=\""+itemValue+"\"]").click();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多