我想提交一個表單到一個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的回答也非常有幫助,但這更詳盡。謝謝! –