提交值到另一个页面iframe表单



我想从iframe提交一个值到另一个页面表单,看起来像这样。

    <input form="form1" type="search><input form="form1" type="submit">
    <iframe src="anotherdomain">
    <form action="foo.php" method="post" id="form1"></form>
    </iframe>

如何做到这一点?我想提交一个值而不重新加载整个页面,而只是在iframe的特定元素。

你不能向"表单"提交数据。这不是表单提交在WWW上所做的。您可以向提交不同站点上的URL(该URL可能与表单使用的URL相同)。只需将action属性设置为绝对URI。(如果目标站点实现了针对CSRF的保护,则可能会失败)。

如果你想提交给一个iframe,那么在表单上使用target属性。

<form action="http://example.com/foo.php" method="post" target="name_of_iframe">
<!-- etc -->
<iframe src="http://example.com" name="name_of_iframe"></iframe>

另一方面,如果你想通过客户端JavaScript操纵其他网站的DOM(例如修改帧中显示的输入值),那么你需要:

  • 您试图操纵的站点的合作
  • postMessage和好友

最新更新