小编Kro*_*ied的帖子

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

我有两页 - "第1页"和"第2页".在第1页上,有一个文本框,其值为100,最后是一个按钮.

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

这是我的Javascript代码:

<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         
}
Run Code Online (Sandbox Code Playgroud)

<script type="text/javascript">

function read_price(){

    alert("started_2");

    alert(price);

}
Run Code Online (Sandbox Code Playgroud)

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

<input class="button_send" id="button_send" type="submit" value="Submit_price" onclick="save_price();"/>
Run Code Online (Sandbox Code Playgroud)

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

但是在第二页上有这个:

window.onload=read_price(); 
Run Code Online (Sandbox Code Playgroud)

我总是得到全球变量价格的"未定义"值.

我已经阅读了很多关于那些全局变量的内容.例如在这个页面:全局变量的问题..但我不能让它工作......

为什么这不起作用?

html javascript variables global undefined

22
推荐指数
2
解决办法
10万
查看次数

标签 统计

global ×1

html ×1

javascript ×1

undefined ×1

variables ×1