如何将值从 iframe 传递给父级? [英] how to pass a value from iframe to the parent?

查看:16
本文介绍了如何将值从 iframe 传递给父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 iframe 上传图片,因此 iframe 包含一个上传按钮.然后它获取保存到数据库所需的图像 ID.在父页面上,我有一个保存按钮.但我希望这个保存按钮被隐藏,直到 iframe 上的上传按钮被点击.如何将值从 iframe 传递给父级?所以我需要一些东西让我知道上传按钮已被点击.然后将该值传递给父级,然后显示保存按钮.框架:

<td><asp:LinkBut​​ton runat="server" ID="btnUpload" CssClass="btnUpload2" OnClick="btnUpload_Click" Text="Upload"/></td></tr><tr><td><asp:FileUpload runat="server" ID="fuUpload"/></td></tr>

<input type="hidden" id="FileNameHidden" style="display:none" runat="server" clientidmode="Static"/>

编辑

所以我在 iframe 的上传函数中添加了 OnClientClick="IsPressed()" 并添加了这个 javascript 代码:

function IsPressed() {varpressed = "是";window.parent.uploadPressed(pressed);}

然后在父级中我添加了这个函数:

function uploadPressed(pressed) {$("#btnUpdateImage").show();}

并将按钮上的可见设置为false:

<asp:LinkBut​​ton ID="btnUpdateImage" Visible="false" onclick="btnUpdateImage_Click" OnClientClick="CloseImage();return GetDbriefFilename();"CssClass="btnSaveSmall" runat="server" ></asp:LinkBut​​ton>

但是当我单击 iframe 上的 uplaod 按钮时,父级上的保存按钮没有显示.我测试过,值正从 iframe 传递给父级,但为什么不显示保存按钮?

解决方案

你必须使用 跨文档消息传递.

例如在顶部窗口中:

myIframe.contentWindow.postMessage('hello', '*');

在 iframe 中:

window.onmessage = function(e){如果(e.data == '你好'){alert('它有效!');}};

I'm using an iframe to upload an image, so the iframe contains an upload button. It then gets the Image ID, which is needed to save to the database. On the parent page I then have a save button. But I want this save button hidden until the upload button on the iframe is clicked. How I can pass a value from the iframe to the parent? So I need something to let me know the upload button has been clicked. Then pass that value to the parent, and then show the save button. iframe:

<tr>
  <td>
    <asp:LinkButton runat="server" ID="btnUpload" CssClass="btnUpload2" OnClick="btnUpload_Click" Text="Upload"  />
  </td>
</tr>
<tr>
  <td>
    <asp:FileUpload runat="server" ID="fuUpload" />                
  </td>               
</tr>
</table>
</div>
<input type="hidden" id="FileNameHidden" style="display:none" runat="server" clientidmode="Static" />

EDIT

So I added OnClientClick="IsPressed()" to the upload function in the iframe and added this javascript code:

function IsPressed() {
  var pressed = "yes";
  window.parent.uploadPressed(pressed);
}

Then in the parent I added this function:

function uploadPressed(pressed) {
  $("#btnUpdateImage").show();
}

And set the visible to false on the button:

<asp:LinkButton ID="btnUpdateImage" Visible="false" onclick="btnUpdateImage_Click" OnClientClick="CloseImage();return GetDbriefFilename();" CssClass="btnSaveSmall" runat="server" ></asp:LinkButton>

But when I click the uplaod button on the iframe, the save button on the parent isn't displaying. I tested and the value is being passed from the iframe to the parent but why isn't the save button displaying?

解决方案

You have to use cross-document messaging.

For example in the top window:

myIframe.contentWindow.postMessage('hello', '*');

and in the iframe:

window.onmessage = function(e){
    if (e.data == 'hello') {
        alert('It works!');
    }
};

这篇关于如何将值从 iframe 传递给父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆