2012-11-06 15 views
6

La iframe è sullo stesso dominio, ho provato il seguente codice, ma nessuna di queste ha funzionato:Invio di un modulo in un iframe con JavaScript

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(); 

Aggiornamento:

Questo è il codice 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 dice:

MyIFrameDoc.getElementById("myform").submit is not a function 

risposta

11

Prova questa:

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

UPDATE

io non riesco a capire il motivo per cui questo non funziona, ma qui è qualcosa che fa:

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

iframe.php:

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

UPDATE 2

Il motivo che stai ricevendo l'errore submit is not a function è perché hai chiamato il vostro pulsante di invio submit, così MyIFrameDoc.getElementById("myform").submit in realtà fa riferimento a un HTMLInputElement, non il HTMLFormElement.submit() metodo.

Tutto quello che dovete fare è rinominare il pulsante di invio, ad es .:

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

Grazie! Sto ricevendo un errore su submit(); Ho aggiornato il mio post con il codice :) – Kristian

+0

@Kristian Penso che funzionerà ora. – Korikulum

+0

Grazie amico, ho aggiunto id e nome al pulsante, ma ottengo MyIFrameDoc.getElementById ("mybutton") è null – Kristian

-4

Molto grazie per il vostro suggerimento. Stavo usando un JavaScript per convalidare alcune informazioni su un modulo prima che fosse inviato. il "MyIFrameDoc.getElementById (" mybutton "). click();" ha funzionato, ma si è scontrato con il mio pulsante di invio che ha chiamato una funzione "onclick()". Per ovviare al problema, ho modificato il mio pulsante di invio originale in una classe di pulsanti con la funzione "onclick()". Poi ho creato un nuovo pulsante di invio type = "submit" con id = "mybutton" con uno style = "width: 1px; height: 1px; border: 0;" in pratica, rendere invisibile il nuovo pulsante di invio e posizionarlo sopra la classe del pulsante con la funzione "onclick()". L'utente fa clic sul pulsante di classe pulsante e lo script "onclick()" convalida le informazioni. Se convalida, gli script di convalida chiamano "MyIFrameDoc.getElementById (" mybutton "). Click();" e invia il modulo.

1

inviare l'URL del Iframe da JavaScript

if (window.parent.$("#IframeId").length > 0) { 
     window.parent.$("#IframeId")[0].contentDocument.forms[0].submit(); 
    } 
Problemi correlati