javascript - 以 iframe 为目标的表单在 Chrome 中打开新标签页

标签 javascript html google-chrome iframe

我正在尝试向 iframe 提交一个表单,这两个表单都是在我的项目中使用 javascript 动态构建的。

<script>
document.querySelector('iframe').setAttribute('id', iframeId);
document.getElementById(iframeId).setAttribute('name', target);
document.getElementById(iframeId).setAttribute('width', width);
document.getElementById(iframeId).setAttribute('height', height);

document.querySelector('form').setAttribute('id', id);
document.getElementById(id).setAttribute('target', target);
document.getElementById(id).setAttribute('action', actionURL);

document.getElementById(id).submit();
<script>

这是它稍后在浏览器 Web Inspector 中的样子。

<iframe id="my_frame" name="my_frame" width="700" height="400">
        <p>Your browser does not support iframes.</p>
</iframe>

<form id="my_form" target="my_frame" action="http://localhost:8080/MyProject/loadService" method="POST" style="display: none;">
    <input type="hidden" name="userId"/>
    <input type="hidden" name="locale"/>
</form>

这对我来说在 Firefox 中效果很好,但在 Chrome 中它会打开一个新标签。我尝试了给出的解决方案 here但它没有解决问题。任何人都可以建议缺少什么吗?

最佳答案

从您的 html 中删除 iframe 并尝试这样做:

//Create the iframe, set its attributes and append it before the form
var iframe = document.createElement('iframe');
iframe.name = 'my_frame';
iframe.id = 'my_frame';
iframe.width = '400';
iframe.height = '700';
var form_element = document.getElementById('my_form');
form_element.parentNode.insertBefore(iframe, form_element);

//Submit form to iframe
document.getElementById('my_form').submit();

关于javascript - 以 iframe 为目标的表单在 Chrome 中打开新标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559283/

相关文章:

javascript - element.style.display = 'none' 在 firefox 中不起作用,但在 chrome 中有效

javascript - 让 webfontloader 与 node.js 和 jsdom 一起工作

javascript - 新闻源的 React 架构

javascript - 仅向使用 Chrome 18 的用户显示单个网页

html - CSS IE 悬停状态问题

java - 如何在selenium中设置chrome代理: Java

javascript - PeerJS 提示调用者姓名?

javascript - 是否可以在 localhost/offline 中运行基于 ajax 的 php 文件

javascript - HTML5 检查音频是否正在播放?

javascript - 当我在 Canvas 上单击并拖动鼠标时,光标会变为文本选择光标。我怎样才能防止这种情况发生?