0

我需要使用 javascript 代码将一个 html 页面的值传递给另一个 html javascript 函数。如何传递价值。提前致谢

view.cshtml:
b.on('click', function () {
    document.location.href =  + '?Id=' + sData; // need to pass the value
});

index.cshtml:
function getId(data) { // i need to get the data here
}
4

2 回答 2

0

您可以通过 Javascript 检索 GET 数据。我创建了一个使用 PHP 样式语法的示例:

<script>
var $_GET = {};
(function() {
    var params=top.location.search.split("?").join("").split("&");
    for(var i=0;i<params.length;i++){
        var param=params[i].split("=");
        $_GET[param[0]]=param[1];
    }
})();

alert($_GET['Id']);

</script>
于 2013-02-26T15:25:40.733 回答
0

你有两个选择:

  • 将数据保存在客户端,例如 cookie。
  • 在 GET 或 POST 请求中将其传递给服务,并获取服务器端脚本(例如 PHP)以将值传入。

通过服务器,如果使用 PHP 为例:

view.cshtml:
// assume sData = 5
b.on('click', function () {
    document.location.href =  + 'index.php?Id=' + sData;
});

当您单击该功能时,它会从服务器请求 index.php。

index.php:
<?php
    ...
    echo "var id = $_POST['Id'];";
    echo "function getId() {";
    // Function code here refering to id variable
    echo "}";
    ...
?>

index.php 生成:

index.cshtml:
var id = 5;
function getId() {

}
于 2013-02-26T15:07:35.253 回答