使用jquery获取另一个页面中变量的值 [英] Get value of a variable in another page using jquery
问题描述
$ 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()
函数来解决这个问题。
可能就像这样
$ 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屋!