【问题标题】:Parent page spawn modal window, button on modal windw cause trigger postback on parent?父页面生成模态窗口,模态窗口上的按钮导致父级触发回发?
【发布时间】:2010-01-29 21:50:22
【问题描述】:

我有一个 asp.net 网络表单。 用户将填写表单上的一些字段。 有一个打开模态窗口的超链接(我正在使用 jquery 和 prettyphoto,但可能会切换到 http://www.ericmmartin.com/projects/simplemodal/,因为他的文档更好)

模态窗口包含一个 iFrame,用户在其中进行选择并保存记录。

此时我需要关闭该窗口并在父页面上触发回发。

目前我在最后一页上有一个超链接,其中 target = parent 和 href = parent page url。 然后我有一个自动点击它的javascript。我知道这是一个廉价的黑客。 然后它将用户发送回父页面,但是页面被重新加载并且所有原始数据都丢失了。 这就是问题所在。

最好的方法是什么?

谢谢!

【问题讨论】:

    标签: jquery modal-dialog simplemodal modalpopups


    【解决方案1】:

    如果父窗口和子 iframe 在同一个域中,那么这应该相当简单。

    在 iframe 内部,window.parent 引用外部 window 对象,在外部页面中,它引用自身。

    https://developer.mozilla.org/en/DOM/window.parent

    所以在 iframe 中,您可以按照以下方式编写代码:

    // Check to make sure we're an iframe
    if (window.parent !== window) {
      // Run an arbitrary function in the parent window
      var daddy = window.parent;
      daddy.doStuffThatSubmitsAFormInTheParentPage();
    }
    // Make sure you have that function defined in the parent page.
    

    这里是jsbin中的访问示例。你需要一个控制台才能看到爱情。

    http://jsbin.com/ucisi/5

    如果你想要它漂亮的相关代码

    http://jsbin.com/ucisi/5/edit

    http://jsbin.com/ucisi/4/edit

    【讨论】:

      【解决方案2】:

      通过 Alex 的解决方案,我能够解决这个问题。

      在父页面上,我有:

      <script language="JavaScript">
      <!--
          function clickPostBackButton() {
              document.getElementById("<%=PostBackButton.ClientID%>").click();
          }
      -->
      </script>
      
      <asp:Button CssClass="hide" CausesValidation="false" runat="server" Text="Postback" ID="PostBackButton" />
      

      然后在模态窗口中加载的 iFrame 上。我有一个带按钮的表格。该按钮将用户带到另一个页面。在这个页面上我有这个代码:

      <script language="javascript" type="text/javascript">
          // Check to make sure we're an iframe
      if (window.parent !== window) {
        // Run an arbitrary function in the parent window
        var daddy = window.parent;
        daddy.clickPostBackButton();
      }
      // Make sure you have that function defined in the parent page.
      
      </script>
      

      此时模态寡妇关闭,父页面触发回发。 重要的是要注意我的按钮有 CausesValidation="false",没有这个我的页面有验证问题。感谢您的提示亚历克斯塞克斯顿

      【讨论】:

      • 我觉得有趣的是,您将您的解决方案列为答案,同时感谢 Alex Slexton 的回应。呵呵。
      • 迈克,wtf。人们正在寻找实际的代码,以便他们可以复制和粘贴它并继续前进。上面的代码就是这样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      相关资源
      最近更新 更多