![](/img/trans.png)
[英]In Javascript, how do I obtain a gridview's current page index?
[英]How do I obtain a JavaScript object that is created on another php file made by the first page?
我的問題是我需要訪問在另一個頁面(php文件)上創建並在當前頁面中使用它的JavaScript對象。
因此,這是我的第一頁test_ong.php,其中包含2個按鈕。 一種是創建另一個生成該JavaScript對象的頁面,另一個創建檢索JavaScript對象的按鈕。
<button id="new">Open New Win</button>
<button id="dis">Display</button>
<div id="test"></div>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var myChild;
$("#new").click(function() {
myChild = window.open("test_reg.php","","width=500, height=500, resizable=yes");
});
$("#dis").click(function() {
console.log('I clicked this');
$.post("test_reg.php", function(data) {
console.log(data);
});
});
</script>
我的第二頁test_reg.php將創建對象whatIWant
。
<script type="text/javascript">
var whatIWant = {
fruit: "apple",
car: "Lotus",
age: 31,
hobby: "coding"
};
</script>
所以,我想要的就是每當我單擊顯示按鈕。 它將在控制台日志中為我提供對象whatIWant
。
我知道我可以通過使用myChild.whatIWant
來獲取此信息,但是我需要刷新我的第一頁並仍然獲取對象myChild.whatIWant
。
感謝您閱讀或回答我的問題的人。
您的第二頁應僅返回如下對象:
{
fruit: "apple",
car: "Lotus",
age: 31,
hobby: "coding"
}
您可以通過ajax調用來做到這一點, 首先需要初始化一個變量,然后通過ajax調用可以覆蓋它 ,然后再次顯示。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script>var whatIWant = {};</script>
</head>
<body>
<button id="new">Open New Win</button>
<button id="dis">Display</button>
<div id="test"></div>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script type="text/javascript">
var myChild;
$("#new").click(function() {
myChild = window.open("test_reg.php","","width=500, height=500, resizable=yes");
});
$("#dis").click(function() {
console.log('I clicked this');
$.ajax({
type: "POST",
url: "test_reg.php",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (errormessage) {
//do something else
}
});
});
</script>
</body>
</html>
這是您的test_Reg.php文件:
echo '<script type="text/javascript">
whatIWant = {fruit: "apple",
car: "Lotus",
age: 31,
hobby: "coding"};
</script>';
也許您可以嘗試將JSON作為會話變量或cookie存儲,然后使用Javascript檢索它。 重新加載頁面后,兩者都應該可以訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.