【问题标题】:using javascript to change the src of iframe does not display new page使用javascript更改iframe的src不显示新页面
【发布时间】:2012-12-04 03:02:54
【问题描述】:

在我的 asp.net 应用程序中,我有一个包含两个 iframe 的页面,并且在此 iframe 中显示的主页面包含一个按钮。当这个按钮被点击时,它会调用一个 javascript 函数,该函数包含 URL 的一部分,并在父窗口中调用一个 javascript 函数。当父函数被命中时,它应该将 iframe 的 src 更改为不同的页面 - 这不会发生。主 iframe 的 ID 为 ContentIframe,侧面的 iframe 的 ID 为 LeftIframe。当它被击中时 - 左侧 iframe src 被更改为空,这是因为左侧显示不显示以前存在的内容。发生更改内容框架的 iframe src 的调用,并且代码隐藏甚至加载(我可以通过在后面代码中的页面加载中放置跟踪点来判断) - 但是页面永远不会显示在 iframe 中,上一页带有我们单击的原始按钮仍在显示中。有谁知道为什么会发生这种情况?这是我的代码

按钮

<asp:Button ID="btnEditClient" onclientclick="editClient();" runat="server" Text="Edit Client Info" />

editClient javascript 函数

function editClient() {
var location = window.location.href;
var idindex = location.indexOf("clientid");
idindex = idindex + 9;
var id = location.slice(idindex, location.length);
window.parent.redir('client', id);

}

redir 父 javascript 函数

function redir(type, id) {
    if (type = 'client') {
        document.getElementById('ContentIframe').src = "NewCustomer.aspx?clientid=" + id;
        document.getElementById('LeftIframe').src = "";

    }
}

如果我要在上面的 LeftIframe src 更改下方添加以下行

document.getElementById('ContentIframe').disabled = true; 

ContentIframe 根本不会显示。同样如上所述,NewCustomer.aspx 页面的页面加载被命中,我可以通过使用断点来判断。再次感谢!

编辑 - 在调用更改 src 之后,查看开发人员工具,这是 iframe 中的表单,仍然显示前几页的项目 - 不确定这是否有帮助 - 也许 method=post 可能有与此有关吗?

<form id="form1" action="NewClientSummary.aspx?clientid=13" method="post">

【问题讨论】:

  • if (type = 'client') { 应该是 if (type == 'client') {
  • 这不是问题,无论哪种方式都会发生同样的事情

标签: javascript asp.net vb.net iframe


【解决方案1】:

问题在于&lt;asp:Button&gt; 被呈现为提交按钮。这意味着单击它将提交表单,并且框架中的表单可能会被定向到同一页面。 (默认操作)

要么使用普通按钮:

<button type="button" id="btnEditClient" onclick="editClient();">Edit Client Info</button>

或者如果你需要它在服务器端,只需通过返回 false 来取消按钮的默认操作:

<asp:Button ID="btnEditClient" onclientclick="editClient(); return false;" runat="server" Text="Edit Client Info" />

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 2023-03-24
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多