通过JavaScript将变量从一个html页面传递到另一个页面

编程入门 行业动态 更新时间:2024-10-28 13:15:13
本文介绍了通过JavaScript将变量从一个html页面传递到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两页 - 第1页和第2页。在第1页上有一个文本框,其值例如是100和一个按钮。

通过按下按钮,我希望javascript将文本框的值保存在全局变量(?)中并跳转到第2页。使用window.onload我想要第二个Javascript函数来提醒页面1保存的值。

这里是我的Javascript代码:

< script type =text / javascript> var price; //在函数=全局变量之外声明? 函数save_price(){ alert(started_1); //仅供参考 price = document.getElementById('the_id_of_the_textbox')。value; alert(price); //仅供参考}

< script type =text / javascript> 函数read_price(){ alert(started_2); alert(price); $ b

在第1页上我有这个发送按钮:

< input class =button_sendid = button_sendtype =submitvalue =Submit_priceonclick =save_price();/>

它启动Javascript函数并将我正确地重定向到我的page2。

但是第二页显示:

window.onload = read_price();

我总是得到全局变量价格的undefined值。

我读了很多关于这些全局变量的信息。例如。在这个页面:全局变量的问题.. 但我无法正常工作...

为什么这不起作用?

解决方案

没有阅读你的代码,只是你的场景,我会通过使用 localStorage 来解决。 下面是一个例子,我将使用 prompt()作为简短。

在页面1上: $ b $ window.onload = function(){ var getInput = prompt(嘿,在这里键入: ); localStorage.setItem(storageName,getInput);

第2页:

window.onload = alert(localStorage.getItem(storageName));

您也可以使用cookie,但localStorage允许更多空间,并且不会将它们发送回服务器当你请求页面。

I have two pages - "page 1" and "page 2". On page 1 there's an text-box with a value of e.g. 100 and a button at the end.

By pressing the button I want javascript to save the value of the textbox in a global (?) variable and jump to page 2. With "window.onload" I want a second Javascript-function to alert the value saved at page1.

Here's my Javascript code:

<script type="text/javascript"> var price; //declare outside the function = global variable ? function save_price(){ alert("started_1"); //just for information price = document.getElementById('the_id_of_the_textbox').value; alert(price); //just for information }

<script type="text/javascript"> function read_price(){ alert("started_2"); alert(price); }

On "page 1" I have this send-Button with:

<input class="button_send" id="button_send" type="submit" value="Submit_price" onclick="save_price();"/>

It starts the Javascript function and redirects me correctly to my page2.

But with this ont the second page:

window.onload=read_price();

I always get an "undefined" value of the global variable price.

I've read a lot about those global variables. E.g. at this page: Problem with global variable.. But I can't get it working...

Why is this not working?

解决方案

Without reading your code but just your scenario, I would solve by using localStorage. Here's an example, I'll use prompt() for short.

On page1:

window.onload = function() { var getInput = prompt("Hey type something here: "); localStorage.setItem("storageName",getInput); }

On page2:

window.onload = alert(localStorage.getItem("storageName"));

You can also use cookies but localStorage allows much more spaces, and they aren't sent back to servers when you request pages.

更多推荐

通过JavaScript将变量从一个html页面传递到另一个页面

本文发布于:2023-05-25 10:59:33,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!