我正在尝试向 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/