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

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

问题描述

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

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

编辑

所以我将 OnClientClick =IsPressed()添加到iframe中的上传功能并添加了这个javascript代码:

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();
}

并在按钮上将visible设置为false:

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>

但是当我点击iframe上的上传按钮时,父节目上的保存按钮不会显示。我测试过,值正从iframe传递给父级,但是为什么不显示保存按钮?

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?

推荐答案

你必须使用跨文档消息

例如在顶部窗口中:

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

和iframe:

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

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

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