问题描述
我有两页 - 第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页面传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!