本文介绍了使用JavaScript在iframe中提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iframe 位于同一个域中,我尝试了以下代码,但没有一个有效:

  myframe.document.getElementById(myform)。submit(); 
parent.top.frames [0] .document.forms [0] .submit();
myframe.document.getElementById(myform)。submit();

MyIFrame = document.getElementById(myframe);
myIFrame.getElementById(myform)。submit();

更新:



这是HTML :

 < html> 
< body>
将IFRAME FRAMEBORDER = 0 SCROLLING = 否 宽度= 530 HEIGHT = 298
SRC = / iframe.php 名称= myframe ID = myframe > ;您的浏览器不支持
< p> iframe。< / p>
< / iframe>< br />

< form action =/ styles.cssmethod =postid =form1>
< input type =textname =input1value =/>
< input type =textname =input2value =/>
< input type =buttonvalue =testonclick =submitFrame()>
< input type =submitvalue =submit>
< / form>

< script>

函数submitFrame(){

var MyIFrame = document.getElementById(myframe);
var MyIFrameDoc =(MyIFrame.contentWindow || MyIFrame.contentDocument);
if(MyIFrameDoc.document)MyIFrameDoc = MyIFrameDoc.document;
MyIFrameDoc.getElementById(myform)。submit(); // ## error

}


< / script>
< / body>
< / html>

iframe.php:

 < form method =postclass =af-form-wrapperid =myformname =myformaction =/> 

< input type =textname =input1value =2073809442/>
< input type =textname =input2value =1/>
< input type =submitname =submitvalue =submit/>
< / form>

Firebug说:

  MyIFrameDoc.getElementById( myForm会)。提交不是函数


试试这个:

  var MyIFrame = document.getElementById(myframe); 
var MyIFrameDoc =(MyIFrame.contentWindow || MyIFrame.contentDocument);
if(MyIFrameDoc.document)MyIFrameDoc = MyIFrameDoc.document;
MyIFrameDoc.getElementById(myform)。submit();

更新



 <$ c 。$ C> MyIFrameDoc.getElementById( myButton的)点击(); 

iframe.php:

 < input type =submitname =submitvalue =submitid =mybutton/> 

更新2



您获得提交的原因不是函数错误是因为您已将提交按钮命名为 submit ,所以 MyIFrameDoc.getElementById( myForm会)。提交实际上引用了一个 HTMLInputElement ,而不是 HTMLFormElement.submit()方法。



您需要做的就是重命名提交按钮,例如:

 < input type =submitname =submit2value =submit/> 


The iframe is on the same domain, I tried the following code, but none of it worked:

 myframe.document.getElementById("myform").submit();
 parent.top.frames[0].document.forms[0].submit();
 myframe.document.getElementById("myform").submit();

 MyIFrame = document.getElementById("myframe");
 myIFrame.getElementById("myform").submit();

Update:

This is the HTML:

<html>
<body>
<iframe frameborder="0" scrolling="no" width="530" height="298"
  src="/iframe.php" name="myframe" id="myframe">
  <p>iframes are not supported by your browser.</p>
</iframe><br />

<form action="/styles.css" method="post" id="form1">
<input type="text" name="input1" value=""/>
<input type="text" name="input2" value=""/>
<input type="button" value="test" onclick="submitFrame()">
<input type="submit" value="submit">
</form>

<script>

function submitFrame(){

var MyIFrame = document.getElementById("myframe");
var MyIFrameDoc = (MyIFrame.contentWindow || MyIFrame.contentDocument);
if (MyIFrameDoc.document) MyIFrameDoc = MyIFrameDoc.document;
MyIFrameDoc.getElementById("myform").submit(); // ## error

}


</script>
</body>
</html>

iframe.php:

<form method="post" class="af-form-wrapper" id="myform" name="myform" action="/"  >

<input type="text" name="input1" value="2073809442" />
<input type="text" name="input2" value="1" />
<input type="submit" name="submit" value="submit" />
    </form>

Firebug says:

 MyIFrameDoc.getElementById("myform").submit is not a function
解决方案

Try this:

var MyIFrame = document.getElementById("myframe");
var MyIFrameDoc = (MyIFrame.contentWindow || MyIFrame.contentDocument);
if (MyIFrameDoc.document) MyIFrameDoc = MyIFrameDoc.document;
MyIFrameDoc.getElementById("myform").submit();

UPDATE

MyIFrameDoc.getElementById("mybutton").click();

iframe.php:

<input type="submit" name="submit" value="submit" id="mybutton" />

UPDATE 2

The reason you're getting the submit is not a function error is because you've named your submit button submit, so MyIFrameDoc.getElementById("myform").submit actually references an HTMLInputElement, not the HTMLFormElement.submit() method.

All you need to do is rename your submit button, e.g.:

<input type="submit" name="submit2" value="submit" />

这篇关于使用JavaScript在iframe中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:25