javascript 从 iframe 提交表单并在提交后关闭它并显示隐藏的 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14645647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:13:31  来源:igfitidea点击:

submit form from iframe and close it after submit and show a hidden div

javascriptformsiframehideshow

提问by user2031113

this is what I have

这就是我所拥有的

    <html>
    <head>
    </head>
    <body>
    <iframe id="myiframe">
    <form id= "myform" action="submit.php" method="POST">
    <input type="submit">
    </form></iframe>
    <div id="hiddendiv" style="display:none">
    some text
    </div>
    </body></html>

and this is what I want to end up with after the form in the iframe is submitted (ether by enter or just clicking the button submit, I do not care because the form only has 1 field)

这就是提交 iframe 中的表单后我想要的结果(通过输入或单击按钮提交,我不在乎,因为表单只有 1 个字段)

    <html>
    <head>
    </head>
    <body>
    //I want this iframe to become display:none or to be deleted
    /*<iframe id="myiframe">
    <form id= "myform" action="submit.php" method="POST">
    <input type="submit">
    </form></iframe>*/
    //and this div to be showed
    <div id="hiddendiv" style="display:">
    some text
    </div>
    </body></html>

can anyone please help, thanks

任何人都可以请帮忙,谢谢

i have the answare but I have to wait for 8 h to post is so I edited the initial question, you can pot is if you want so I can validate it as a answare

我有 answare,但我必须等待 8 小时才能发布,所以我编辑了最初的问题,如果你愿意,你可以锅,所以我可以将它验证为 answare

got it

知道了

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

function closeIframe() {
var iframe = document.getElementById('myiframe');
iframe.parentNode.removeChild(iframe);
document.getElementById("hiddendiv").style.display="";
}

</script>
<iframe id="myiframe">
<form id= "myform" action="submit.php" method="POST">
<input type="submit">
</form></iframe>
<div id="hiddendiv" style="display:none">
some text
</div>
</body></html>

and in the submit.php the return is this

在 submit.php 中,返回是这样的

echo '<script type="text/javascript">';
echo 'parent.closeIframe()';
echo '</script>';

采纳答案by user2031113

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

function closeIframe() {
var iframe = document.getElementById('myiframe');
iframe.parentNode.removeChild(iframe);
document.getElementById("hiddendiv").style.display="";
}

</script>
<iframe id="myiframe">
<form id= "myform" action="submit.php" method="POST">
<input type="submit">
</form></iframe>
<div id="hiddendiv" style="display:none">
some text
</div>
</body></html>

and in the submit.php the return is this

在 submit.php 中,返回是这样的

echo '<script type="text/javascript">';
echo 'parent.closeIframe()';
echo '</script>';

回答by mplungjan

If from same domain, add this in the submit.php and extract the form to put in the iframe You need

如果来自同一个域,在 submit.php 中添加这个并提取表单以放入 iframe 你需要

<iframe src="form.html"></iframe>

DEMO

演示

<script>
top.document.getElementById("myiframe").style.display="none";        
top.document.getElementById("hiddendiv").style.display="block";
</script>