0

我的 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

任何帮助将不胜感激。

顶级PHP:

<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>

入口PHP:

<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>

加载PHP:

<?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>

谢谢

4

1 回答 1

1

您已经changeDiv()在 jQueryready()函数中定义了函数,因此它只存在于该范围内,并且不能作为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>
于 2013-03-25T17:06:01.327 回答