使用 jquery 获取另一个页面中变量的值

sss*_*s05 2 jquery get

我真的很震惊。实际上问题是,我有 2 个网页名称为 menu.html 和 index.html。在 menu.html 中,我有 3 个按钮,例如“一、二、三”,当我单击任何一个按钮时,它会重定向到 index.html。所以,我写了一个 common.js 文件来获取我在 menu.html 中单击的按钮的值。我已将 common.js 包含到 menu.html 和 index.html 中。

常见的.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
        });
}); 
Run Code Online (Sandbox Code Playgroud)

现在我需要在 index.html 中的 another.js 文件中获取“文本”的值。

另一个.js:

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

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

<script src="common.js" type="text/javascript"></script>
<script src="another.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

这里做错了什么?提前谢谢你。

viy*_*ncs 5

您可以使用localStorage()函数,localstorage 会像 cookie 一样保存您的数据,但它非常强大,因为 localstorage 可以保存比 cookie 更大的数据。

在你的情况下可能是这样的

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

然后在另一个页面中,您只需使用此通知

$(function(){
 alert("text" + localStorage.getItem("text"));
});
Run Code Online (Sandbox Code Playgroud)

这是本地存储的详细信息