如何将值从iframe传递给父级?

时间:2015-09-01 09:57:17

标签: javascript jquery html iframe

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

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

然后在父母中我添加了这个功能:

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您必须使用cross-document messaging

例如在顶部窗口中:

&#13;
&#13;
myIframe.contentWindow.postMessage('hello', '*');
&#13;
&#13;
&#13;

并在iframe中:

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

相关问题