簡體   English   中英

將值從一個html頁面傳遞到另一個html javascript函數

[英]passing the value one html page to another html javascript function

我需要使用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
}

您有兩種選擇:

  • 將數據保存在客戶端,例如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() {

}

您可以通過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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM