javascript 如何获得 Iframe 提交表单的响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10735612/
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 10:52:04  来源:igfitidea点击:

how to get the response of Iframe submitting a form?

javascriptjqueryformsiframeresponse

提问by Rahul

I have a form that has been called using a iframe, now when that form is submitted i want to catch a response in the main site from where the iframe was called and depending on it would enable/disable a button in main page.
Please anyone help me out..

我有一个使用 iframe 调用的表单,现在当提交该表单时,我想从调用 iframe 的主站点中捕获响应,并根据它启用/禁用主页中的按钮。
请任何人帮助我..

回答by Matthias Ehinger

You could use window.postMessage API. Its is a HTML5 feature and not all browsers support this feature

您可以使用 window.postMessage API。它是 HTML5 功能,并非所有浏览器都支持此功能

In the Iframe and Parent Site you need a check if the browser does support postMessage

在 Iframe 和父站点中,您需要检查浏览器是否支持 postMessage

if (typeof window.postMessage != 'undefined') { }

you can send your message with

你可以发送你的消息

window.parent.postMessage("test","*");

In your Parent (Main Site) you need an eventlistener

在您的父(主站点)中,您需要一个事件侦听器

window.addEventListener('message', receive, false);

function receive(evt)
{
  // handles the event
}

回答by Ja?ck

Just use parentfrom within the iframe once the form is submitted.

只需使用parent从一次提交表单的iframe中。

parent.document.getElementById('xx').innerHTML = "hello world";