我想提交一个表单到一个iframe,这两个都是在我的项目中使用javascript动态构建的。形式瞄准一个iframe在Chrome中打开新标签
<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但它没有解决问题。任何人都可以请建议缺少什么?
@jeremypress的回答也非常有帮助,但这更详尽。谢谢! –