使用jquery获取另一个页面中变量的值 [英] Get value of a variable in another page using jquery

查看:1048
本文介绍了使用jquery获取另一个页面中变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很受打击。其实问题是,我有2个网页名称为menu.html& index.html的。在menu.html我有3个按钮像一,二,三,当我点击任何一个按钮,它会重定向到index.html。所以,我写了一个common.js文件来获取我在menu.html中点击的按钮的值。我已经将common.js包含在menu.html& index.html的。
$ b common.js:

  var text =; 
$(document).ready(function(){
$('input:button')。click(function(){
text = $(this).val();
alert(text); //在这里为menu.html页面中的'one,two或3'按钮获取正确的值
});
});

现在我需要在索引中的another.js文件中获取text的值。 html。



another.js:

 <$ c $ $> $(document).ready(function(){
alert(text+ text); //在这里获得空值
});

此外,我在index.html中正确包含:

 < script src =common.jstype =text / javascript>< / script> 
< script src =another.jstype =text / javascript>< / script>

这里做错了什么?您可以使用 localStorage()函数来解决这个问题。

,localstorage会保存你的数据,比如cookie,但是它非常省力,因为localstorage可以保存比cookie更大的数据。

在你的情况下,

可能就像这样


$ b $

  $(document).ready(function(){
$('input:button')。click(function(){
localStorage.setItem(text,$(this).val());
});
});

然后在您刚刚通知的另一个网页中

  $(function(){
alert(text+ localStorage.getItem(text));
});

详细信息本地存储


am really struck here. Actually the problem is, i have 2 web pages name as menu.html & index.html. in menu.html i have 3 buttons like "one,two,three", when i click any of a button it'll redirect to index.html. so, i wrote a common.js file for getting value of what button i clicked in menu.html. i have included common.js into both menu.html & index.html.

common.js :

var text=" ";
$(document).ready(function() {
    $('input:button').click(function() {
        text = $(this).val();
        alert(text); //here am getting correct values for a button like 'one,two or three' in menu.html page
        });
}); 

now i need to get the value of "text" in another.js file which is in index.html.

another.js:

$(document).ready(function() {
    alert("text "+text); //here am getting empty value.
});

moreover i included correctly in index.html :

<script src="common.js" type="text/javascript"></script>
<script src="another.js" type="text/javascript"></script>

what am doing wrong here? thank u in advance.

解决方案

you can use localStorage() function, localstorage will be save your data like cookie, but it's very powefull because localstorage can be save data more big than cookie.

in your case maybe like this

$(document).ready(function() {
    $('input:button').click(function() {
        localStorage.setItem("text",$(this).val());
        });
}); 

and then in another page you just notify with this

$(function(){
 alert("text" + localStorage.getItem("text"));
});

this is for details local storage

这篇关于使用jquery获取另一个页面中变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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