在iframe中发布表单 [英] POST a form in an iframe

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

问题描述

我想在iframe中发布一个表单,像这样生成:



我的JS在页面内加载一个iframe,向iframe添加一个表单并提交表格。我想要发生的是加载该请求结果的iframe。因此,我希望有效地发布表单并在iframe中呈现结果 ,而不触及父级(除了将iframe放在第一位显示外)。



我使用这个答案中的代码:

JavaScript post request like a form submit



但我无法让它不能重新加载父项。我发布表单,而不是iframe刷新,整个父级刷新。我不知道这是为什么,因为它发布的网址是不同的,至少会重定向到那里。



任何人都可以帮我解决这个问题吗?我只想在iframe中放置一个帖子,并且只在iframe中,基本上。编辑:经过一些更多的研究,显然表单没有被正确创建。我使用了document.createElement(form),然后是document.getElementById(my_iframe_id)。appendChild(form)来追加它,但它似乎没有正常工作。

解决方案

正确,因为您正在当前文档中创建表单节点。

 的document.getElementById( my_iframe_id)contentWindow.document.createElement( '形式')。 

在iframe中创建它。


I would like to POST a form in an iframe, generated like so:

My JS loads an iframe inside the page, adds a form to the iframe and submits the form. What I would like to happen is the iframe to load the result of that request. So, I would effectively like to post a form and render the result inside the iframe, without touching the parent (apart from putting the iframe up for display in the first place).

I am using the code from this answer:

JavaScript post request like a form submit

but I can't get it to not reload the parent. I post the form, and instead of the iframe refreshing, the entire parent refreshes. I don't know why that is, since the url it's posting to is different and would at least redirect there.

Can anyone help me with this problem? I just want a post inside an iframe and only within the iframe, basically.

EDIT: After some more research, apparently the form is not being created properly. I'm using document.createElement("form") and then document.getElementById("my_iframe_id").appendChild(form) to append it, but it does not seem to be working correctly.

解决方案

Correct, because you are creating the form node in the current document.

document.getElementById("my_iframe_id").contentWindow.document.createElement('form'); 

to create it inside the iframe.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆