Jquery Iframe调用Top或Parent函数

时间:2013-03-25 16:55:42

标签: jquery function iframe

我的iframe发送变量给它的父函数时遇到问题。 topPHP.php是我的父,它有一个导致我问题的函数和一个加载entryPHP.php的iframe,这是一个将pdf文件的帖子发送到loadPHP.php的表单,它将pdf上传到我的数据库并调用函数父topPHP.php。对不起,有一句话。我得到的结果如下:

when topPHP.php loads the div topDiv reads "No Control"
I choose a file in the iframe (src = entryPHP.php) and hit submit
loadPHP.php is called and the file is upload to the database perfectly
In the Javascript/Jquery the alert "before the function" is called and works
After that the div topDiv still reads "No Control" and the second alert "after the function" is not called

非常感谢任何帮助。

topPHP:

<script type="text/javascript" src="../jQuery.js"></script>
<script type="text/javascript">

$( document ).ready(function() {
num = 1;

function changeDiv(num){
if(num==1)
{
$("#topDiv").val("top control");
}
if(num==2)
{
$("#topDiv").val("bottom control");
}
}
}
</script>

<iframe id="iframe_display" src="entryPHP.php" width="400" height="100">
</iframe>

<div id="topDiv">
no control
</div>

entryPHP:

<div id="uploadPDF">
<form enctype="multipart/form-data" method="POST" action="loadPHP.php">
<table width="300" height="25" border="1">
//this table sends a pdf file as a post to loadPHP.psh
</table>
</form>

loadPHP:

<?php
//this uploads the pdf file to the database
?>

<script type="text/javascript" src="../jQuery.js"></script>
<script type="text/javascript">
$( document ).ready(function() {

$(window).load(function(){

});
alert("before the function");
parent.changeDiv(2);
alert("after the function");

});
</script>

由于

1 个答案:

答案 0 :(得分:1)

您已在jQuery changeDiv()函数中定义了ready()函数,因此它仅存在于该范围内,并且无法作为parent对象的一部分进行访问。

要解决此问题,请将该功能移至topPHP页面的全局范围,如下所示:

<script type="text/javascript">
function changeDiv(num){
    if(num==1)
    {
        $("#topDiv").val("top control");
    }
    if(num==2)
    {
        $("#topDiv").val("bottom control");
    }
}
$( document ).ready(function() {
    changeDiv(1);
}
</script>