我有一个名为parent.html的页面。在那里,我将模式对话框称为child.html。我在child.html中有一个用于关闭模式对话框的链接按钮。同时,我希望将值从child.html传递给parent.html。
如何将值从子页面发送到父页面?
发布于 2012-12-19 15:32:11
如果子页面( child.html )与父页面位于同一域中,则可以从child.html调用父窗口的函数。
在子页面(child.html)中:
<a href="..." onclick="parent.callFromChildPage('ABC')">Button Title</a>并且,在父页面(parent.html)中:
<script language="javascript">
function callFromChildPage(a_value){
alert("A value from child window is :" + a_value); // 'ABC'
}
</script>此外,如果您可以在子页面(child.html)中使用jQuery,则可以直接设置父页面的元素,如下所示:
<a href="..." onclick="$('#test', window.parent.document).html('ABC');">Button Title</a>发布于 2012-12-19 15:48:08
基本上,在父页面中定义一个javascript函数,然后在子页面中使用opener调用此函数。
window.opener允许您引用打开文档的窗口对象。
子HTML:
<html>
<head>
<script type="text/javascript">
$('a#child-link').click(function(e){
e.preventDefault();
var tmp = $('input#child-input').value();
window.opener.getChildVar(tmp);
});
</script>
</head>
<body>
<input type="text" name="child-text" id="child-input" value="test text" />
<a href="#" id="child-link">link</a>
</body>
</html>父HTML:
<html>
<head>
<script type="text/javascript">
function getChildVar(val) {
var received_val = val;
}
</script>
</head>
</html>https://stackoverflow.com/questions/13947458
复制相似问题