【问题标题】:JavaScript / Jquery : how to access a form present inside a iframe from parent windowJavaScript / Jquery:如何从父窗口访问 iframe 中存在的表单
【发布时间】:2014-09-19 06:55:28
【问题描述】:

我有这种情况,我有一个父窗口(表单名称:pform),单击父窗口上的按钮,我向用户显示一个 iframe。 Iframe 最初是父窗口的一部分,但有一个工具可以取消 Iframe。 Iframe 有一个单独的表单。取消停靠是指整个 iframe 与表单一起复制到一个新窗口中(表单名称:cform)。

如何从父窗口评估 Iframe 中的隐藏变量(id="r111.o1" 和 "r111.o2")。

<form method="post" action="xxx" name="pform" id="pform">
    <div>
    <div class="iframe">
    <iframe>
    <form name="cform" id="cform" >
    <input type="hidden" value="1" name="r111.o1" id="r111.o1">
    <input type="hidden" value="1" name="r111.o2" id="r111.o2">
    ...
</form>

iframe 取消停靠后,

<form method="post" action="xxx" name="pform" id="pform">
    <div>
    <div class="iframe">
</form>


<html><div><iframe>
    <form name="cform" id="cform" >
    <input type="hidden" value="1" name="r111.o1" id="r111.o1">
    <input type="hidden" value="1" name="r111.o2" id="r111.o2">
    ....

我怎样才能仍然能够访问隐藏变量?这可能吗??

更新: 我已经编写了这个 JS 代码,我可以在停靠或取消停靠时识别窗口/iframe。

XT.Assess = {};
XT.Assess.windows = new Array(); // creating array to keep track of the child window ( undocked) 
XT.Assess.iframes = new Array(); // creating array to keep track of the iframe ( docked) 
function validateRub(id) {
    var rubWindow = XT.Assess.windows[id];
    if( rubWindow ){ // undocked
    console.log( 'rubWindow = '+rubWindow );
    var rubcont = rubWindow.contentDocument || rubWindow.contentWindow.document; 
    console.log( 'rubcont = '+rubcont ); 
    var rubIframe = XT.Assess.iframes[id]; // not working
    console.log( 'rubrIframe = '+rubrIframe );
}
else { //docked
    var rubIframe = XT.Assess.iframes[id];
    console.log( 'rubIframe = '+rubIframe );
    var ifr = document.getElementById('Iframe0');
    console.log( 'ifr= '+ ifr);
    var ifrDoc = ifr.contentDocument || ifr.contentWindow.document;
    console.log( 'ifrDoc= '+ifrDoc );
    var theForm = ifrDoc.getElementById('cform');  // Iframe
    console.log( 'theForm= '+theForm );
}
return false;
}

我现在只需要一个代码来遍历对象rubIframe 和rubWindow 中的HTML 元素。我走在正确的道路上吗? 提前致谢。

【问题讨论】:

标签: javascript jquery forms


【解决方案1】:

您可以通过使用 iframe contentwindow 元素来使用 iframe 内容

var iframe = document.getElementById("yourFrameId");
var iframeContent = (iframe.contentWindow || iframe.contentDocument);

然后您可以调用该文件的任何函数,例如

iframeContent.myfunction();

【讨论】:

  • 你试过了吗?我无法从上面的 else 部分访问我在 cform 中的函数。var is_rub_incomplete = ifrDoc.checkRub();。在 FF firebug 中,我看到错误:TypeError: ifrDoc.checkRub is not a function
  • 我正在尝试使用document.getElementById("cform") 直接从其他部分访问表单中的隐藏变量。这也行不通。
  • 是的,我在我的项目中使用了几次,效果很好
  • 不,您只能在获取其内容窗口或相关文档后在您的父文件中使用 iframe 函数和变量
  • 试试这个 var iframeContent = (document.getElementById("cform").contentWindow || document.getElementById("cform").contentDocument);然后 var is_rub_incomplete = iframeContent .checkRub();
猜你喜欢
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
相关资源
最近更新 更多