【问题标题】:Pass radiobutton value from one html page to another as parameter and extract it on next html page将单选按钮值从一个 html 页面传递到另一个作为参数,并在下一个 html 页面上提取它
【发布时间】:2016-04-04 11:38:32
【问题描述】:

我是 HTML 新手。我已经编写了下面的代码以从其中一个选项中进行选择。选择一个单选按钮并单击提交按钮后,用户应重定向到考试页面,其中来自单选按钮值的候选人类型在 URL 中传递,该 URL 必须在下一页提取。

有人可以帮我解决这个要求,以传递价值并在下一页提取它。

<html>
    <head>
        <title>Online Examination Portal</title>
        <h1>Online Examination</h1>
        <script type="text/javascript">
            function get_action(form) {
            form.action = document.querySelector('input[name = "candidateType"]:checked').value;
        }
        </script>
    </head>
    <body>
        <div>Select candidate type from below option:<br><br>
            <div>
                <input type="radio" name="candidateType" value="student">Student
                <br>
                <input type="radio" name="candidateType" value="professional">Professional  
                <br><br>
                <form action="ExamPage.html" method="get"><input type="submit" value="Submit" onclick="get_action(this);"></form>           
                <br><br>
                <form action="RegistrationPage.html" method=post name="form2"><input type="submit" value="Register"></form>
            </div>
        </div>
    </body>
</html>

【问题讨论】:

标签: javascript html


【解决方案1】:

您可以使用 Cookie 或 LocalStorage,其中LocalStorage 更易于实施,但需要最新的浏览器,并且用户可以出于隐私原因禁用 cookie。

Cookies

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// First Page
setCookie("myinputvalue", document.getElementsByName("candidateType")[0].value, 10);

// Second Page
getCookie("myinputvalue");

本地存储

if (typeof(Storage) !== "undefined") {
  // First Page
  localStorage.setItem("myinputvalue", document.getElementsByName("candidateType")[0].value);
  // Second Page
  localStorage.getItem("myinputvalue");
} else {
  // Sorry! No Web Storage support..
  // Use the above cookie method.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2013-04-13
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多