下面的表单.submit()
会打开一个新窗口,尽管form target='my-form-target
,iframe中仍然没有显示任何内容。请参阅小提琴:https://jsfiddle.net/q81Lgmmf/1
createAndSubmitForm();
function createAndSubmitForm() {
var formBody = '<form action="//fiddle.jshell.net"><input value="123"></form>';
var formContainer = document.createElement('div');
var formTarget = document.createElement('iframe');
document.body.appendChild(formContainer);
document.body.appendChild(formTarget);
formTarget.name = 'my-form-target';
formContainer.innerHTML = formBody;
// setting form target to the iframe
// this sets the action, but iframe does not receive anything on submit
formContainer.children[0].target = formTarget.name;
// submit comes in a new window as if target were _blank
formContainer.children[0].submit();
}
我需要通过JS将新创建的表单的目标设置为新创建的iframe。
答案 0 :(得分:0)
再次回答我自己的问题。在设置目标关系之前将新表单/ iframe附加到文档时,会发生此错误。我不清楚确切的顺序,但最后做的附加做了诀窍:https://jsfiddle.net/q81Lgmmf/3/
formTarget.name = 'my-form-target';
form.target = 'my-form-target';
document.body.appendChild(formTarget);
document.body.appendChild(form);