如何使用JavaScript将页面中的值传递给另一个页面 [英] How to pass an a value from a page to another using javascript

查看:89
本文介绍了如何使用JavaScript将页面中的值传递给另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在HTML中使用javascript将值或数组从一个页面传递到另一个页面。我不允许使用本地存储或会话,只能通过页面之间的变量传递。我从单选按钮发送值。我打算将结果存储在数组中,因为我正在跟踪用户的答案,并在最后显示结果。我如何将数组发送到 quiz_5.html ?我打算继续传递数组,而不是使用cookie或本地存储,因为我不允许。

How do you pass a value or an array from one page to another in html using javascript. I'm not allowed to use local storage or sessions only pass the variable from page to page. I'm sending values from a radio button. I intend to store the results in array as i am keeping track of the users answer to display the result at the end. How do i send an array to quiz_5.html? I intend to keep passing the array instead of using a cookie or local storage as i am not permitted to.

以下是我的代码:

<div>
    <form>
        <input type="radio" name="radio" value="correct" class="firstRow"> NASA.Gov
        <input type="radio" name="radio" value="incorrect" class="secondRow"> Data.Gov <br>
        <input type="radio" name="radio" value="incorrect" class="firstRow"> Facebook
        <input type="radio" name="radio" value="incorrect" class="secondRow"> XYZ.net <br>

        <input type="button" value="Submit & Next Question" onclick="getAnswer4(this.form)" class="firstRow">
        <input type="button" value="Cancel & Clear Selection" onclick="clearOptions(this.form)" class="secondRow">
    </form>
</div>

Javascript代码:

Javascript code:

function getAnswer4(form) {
    var a[];
    var value;

    var checked = form.querySelector("input[type=radio]:checked");

    if(!checked) {
        alert('Please select an answer');
        return;
    }
    else{
        value = checked.value;

    }
    a.push(value);
    location.href = "quiz_5.html";
}


推荐答案

,但我都是关于新编码员自己学习基础知识而不依赖提供答案的。只是为了表明它可以完成 - 我刚刚创建了三个HTML页面。在前两个表单中创建一个表单 - 每个表单都包含您的问题(第一页中的问题1和问题2以及第二页中的问题3),并将表单值传递给下一个表单值,仅使用html。

Sorry if my comments seemed harsh, but I am all about new coders learning the basics themselves without relying on having the answers provided. Just to show it can be done - I just created three HTML pages. Created a form in the first two - each with your questions (questions 1 and 2 in the first page and question 3 in the second page), and passed the form values to the next one, using nothing more than html.

然后在第二和第三页上只使用JavaScript,我从网址中抓取了这些值,并用它们做了一些事情。在第二页中,我重新使用了第1页中的值(想想如何完成以及为什么它有用),以便将所有三个值传递给第3页,然后使用JavaScript仅抓取3个值,显示它们在页面上(如下面的代码部分所示)并计算正确答案的总数和百分比。请注意,我回答了问题,因此我得到的问题2不正确。

Then using only JavaScript on the second and third pages, I grabbed the values out of the URL and did stuff with them. On page two, I re-used the values from page 1 (think how that might have been done and why it is useful) so that all three values are passed to page 3 which then used JavaScript only to grab the 3 values, display them on the page (as shown in the code section below) and calculate the total and the percentage of answers that are correct. Note that I answered the questions so that I got question 2 incorrect.

请注意,我不是给出使用的代码,但会给你网页的网址,以便您可以看到前两页的结果,然后开始思考我是如何实现这一目标的。

Note that I am not giving the code used, but will give you the URL of the pages so that you can see the outcome of the previous two pages and can then start to think how I achieved this.

numbers1.html
numbers2.html?one=correct&two=incorrect
numbers3.html?one=correct&two=incorrect&three=correct

Question 1: correct

Question 2:incorrect

Question 3:correct

2/3

0.67% correct

不是我知道的传统答案,但对于学习者只是要求提供答案并不理想,特别是在10分钟内,我能够将这三页结果汇总在一起。如果你不尝试,那么你就不会为自己学习。

Not a traditional answer I know, but it is not ideal for learners simply ask for the answer to be provided, especially when in 10 minutes I was able to put together the three pages that achieved the outcome. If you do not try then you will not learn for yourself.

一般注意:两个论坛成员和我花了很多时间在今天下午试图帮助OP与需要的代码 - 给出代码示例。所以这篇文章更多的是关于教学而不是提供答案。我希望我有这样的老师来激发研究和理解,然后再问问答案。所以,现在我已经说过了 - 随意投票,但我支持我的理念,不会删除这个答案,只是为了不接受低价。此论坛必须是教学环境以及代码库。

General note: two forum members and I spent a lot of time this afternoon trying to assist the OP with the code required - giving code examples. so this post is more about teaching than providing answers. I wish I had teachers like that to inspire research and understanding before simply asking for the answer. So now that I have said that - feel free to down vote, but I stand behind my philosophy and will not delete this answer, simply to not get downvotes. This forum must be a teaching environment as well as a code repository.

这篇关于如何使用JavaScript将页面中的值传递给另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆