javascript - 将 var 传递给另一个页面

标签 javascript php jquery

是否可以将 totalScore var 传递给另一个页面 onclick 以便在那里显示?例如:点击提交链接,它会转到 yourscore.html 并在页面上显示分数

$("#process").click(function() {   
    var totalScore = 0;
    $(".targetKeep").each( function(i, tK) {
       if (typeof($(tK).raty('score')) != "undefined") {
          totalScore += $(tK).raty('score');
       }
    });
    alert("Total Score = "+totalScore);
});

最佳答案

让我们假设您的 HTML 可能如下所示:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

  <script>
    $(document).ready(function(){

      $("#process").click(function() {   
        var totalScore = 0;
        /*
         Your code to calculate Total Score
         Remove the next line in real code.
        */
        totalScore = 55; //Remove this

    alert("Total Score = "+totalScore);
      $("#submit-link").attr('href',"http://example.com/yourscore.html?totalScore="+totalScore);  
});


    });
  </script>

<meta charset=utf-8 />
<title>JS Bin</title>

</head>
<body>
  <button id="process">Process</button>
<br />
<a href="#" id="submit-link">Submit Total Score</a>

</body>
</html>

看看这个 DEMO

在 yourscore.html 中,您可以在以下问题中了解更多信息,以从 URL 中提取 URL 参数:

Parse URL with jquery/ javascript?

关于javascript - 将 var 传递给另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855680/

相关文章:

PHP 数组到 JavaScript

javascript - 我应该用什么来代替 ng-repeater

php - Codeigniter 中的路由 - 自动

PHP-MySQL 查询 -- 请解释

javascript - jQuery:中断 fadeIn()/fadeOut()

javascript - jquery 在可排序时更改类

javascript - ReactJS,在 React 组件中按类名查找元素

javascript - 测试嵌套 JavaScript 对象键是否存在

php - Composer 更新与 Composer 更新 --dev

javascript - 使用 jquery 滚动到页面上的特定元素