重定向后如何检查单选按钮

how do I check a radio button after being redirected

本文关键字:检查 单选按钮 何检查 重定向      更新时间:2023-09-26
重定向

到新网页后如何检查单选按钮?

我试过这个脚本:

<script>
    function AffStatus(choix) {
        document.getElementById(choix).checked = true;
        document.location.href = "PAGELINK?tri="+ choix;
    }
</script>

使用这些单选按钮

Sort by : <input type="radio" name="choice" id="ddc" value="ddc"
                on="AffStatus('ddc')" checked />Activity
            <input type="radio" name="choice" id="di" value="di"
                onclick="AffStatus('di')" />Date  <input
                type="radio" name="choice" id="nm" value="nm"
                onclick="AffStatus('nm')" />NBM <input
                type="radio" name="choice" id="p" value="p" onclick="AffStatus('p')" />Pseudo
            (A-Z)

PS:我正在将用户重定向到同一显示的页面。

编辑:我正在使用Eclipse而不是PHP使用Java EE进行编程。

但它对我不起作用。我认为我写了一个错误的脚本,我无法解决它。请帮忙吗?

实际上,不需要单独的代码来检查具有相同名称的单选按钮。它将自动取消选中其他并选中新的。

我发现一个错误,你有 on="AffStatus('ddc')" 而不是 onclick="AffStatus('ddc')"

我想知道你为什么不使用Jquery,

编辑:

由于您想在刷新后选择无线电,并且不使用后端代码来获取 URL 参数"tri",因此此函数将帮助您。

function AffStatus(choix) {
    document.getElementById(choix).checked = true;
    document.location.href = "PAGELINK?tri="+ choix;
}
window.onload=function(){
    var selected_radio = getURLParameter('tri');
    console.log(selected_radio)
    document.getElementById(selected_radio).checked = true;
}
function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/'+/g, '%20'))||null
}

使用此函数从链接中获取tri

功能:

var RequestQuerystring;
(window.onpopstate = function () {
var match,
    pl = /'+/g,  // Regex for replacing addition symbol with a space
    search = /([^&=]+)=?([^&]*)/g,
    decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
    query = window.location.search.substring(1);
RequestQuerystring = {};
while (match = search.exec(query))
    RequestQuerystring[decode(match[1])] = decode(match[2]);
})();

用法:

var myTri = RequestQuerystring.tri // your tri from the link 

然后将myTri传递给您的Checked