2013-02-26 92 views
0

我需要將值爲1的html頁面傳遞給使用javascript代碼的另一個html javascript函數。如何傳遞價值。在此先感謝將值爲1的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 
} 
+1

請張貼一些代碼,你正在嘗試做的細節,到目前爲止,你已經嘗試過的東西。 – benzonico 2013-02-26 15:06:23

+0

使用查詢字符串進行傳遞,將值從一個html頁面傳遞到另一個html頁面的簡單方法 – 2013-02-26 15:07:22

+0

有許多解決方案從** cookies **到** websql **(僅舉幾個[two ;-)] ),平臺和限制是什麼? – 2013-02-26 15:10:37

回答

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() { 

} 
+0

查看我編輯過的帖子mjshaw – 2013-02-26 15:14:28

+0

已經提到cshtml – 2013-02-26 15:23:16

+0

請在我的代碼中修改。我沒有明白 – 2013-02-26 15:28:36

0

你可以通過Javascript檢索獲取數據。 我創建它使用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> 
+0

我把那個代碼。索引或視圖 – 2013-02-26 15:30:28

+0

根據您的代碼示例,我會將其放在'getId'函數上方(不包含