[英]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.