简体   繁体   English

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

[英]how do I check a radio button after being redirected

How can I check a radio button after being redirected to a new web page ? 重定向到新网页后如何查看单选按钮?

I've tried this script : 我已经尝试过以下脚本:

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

With these radio buttons 使用这些单选按钮

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: I'm redirecting the user to the same displayed page. PS:我将用户重定向到相同的显示页面。

EDIT: I'm programming with Java EE using Eclipse not PHP. 编辑:我正在使用Java EE而不是PHP使用Eclipse进行编程。

But it doesn't work for me. 但这对我不起作用。 I think that I've wrote a wrong script that I can't a solution to fix it. 我认为我写了一个错误的脚本,无法解决该问题。 Any help please ? 有什么帮助吗?

Actually there is no need to have separate code to check the radio buttons with same names. 实际上,不需要使用单独的代码来检查具有相同名称的单选按钮。 It will be automatically uncheck other and check the new one. 它将自动取消选中其他并选中新的。

I found a mistake that you have on="AffStatus('ddc')" instead of onclick="AffStatus('ddc')" 我发现您的错误是on =“ AffStatus('ddc')”而不是onclick =“ AffStatus('ddc')”

I wonder why you are not using Jquery, 我想知道为什么您不使用Jquery,

EDITED: 编辑:

since you want to select the radio after refresh and you dont use the backend code to get the URL param 'tri', this function will help you.. 由于您要在刷新后选择无线电,并且您不使用后端代码来获取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
}

Use this function to get tri value from your link 使用此功能从链接中获取tri

Function : 功能说明

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]);
})();

Usage : 用法:

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

then pass myTri to your Checked 然后将myTri传递给您的Checked

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 浏览器刷新后,如何在不使用cookie的情况下保留单选按钮和复选框的值? - How do I preserve radio button and check box value without using cookies after the browser refresh? 如何检查是否使用JavaScript检查了单选按钮? - How do I check if a radio button is checked using JavaScript? 如何检查我的单选按钮是否已选中并且循环正常工作? - How do I check if my radio button is selected and the loop is working? 如何获得自定义单选按钮进行检查? - How do I get a custom radio button to check? 当有多个具有相同名称的单选按钮时,如何检查选择了哪个单选按钮? (使用jquery) - How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery) 如何使用Javascript检查其他单选按钮中是否选中了特定的单选按钮? - How do i check if a specific Radio button is checked among the other radio buttons using Javascript? 如何检查Jinja2 HTML模板中选中的单选按钮中的哪个单选按钮? - How do I check which radio button in a group of radio buttons is selected in a Jinja2 HTML Template? 如果用户未选中复选框或单选按钮,如何为复选框或单选按钮设置一些默认值 - how do I set some default value for a check box or a radio button if user do not check the check box or select a button 默认情况下,如何检查单选按钮? - How can i check radio button by default? 如何检查单选按钮是否被选中? - How can I check if a radio button is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM