当我试图从另一个页面提交表单时,我遇到了一个问题,这个页面加载在一个DOM元素中。所述的form元素具有以下属性,如下所示:
<form id="thisForm" enctype="multipart/form-data" method="post" action="some.php" accept-charset="UTF-8">
<table>
<tbody>
<tr>
<th><b>Title 1</b></th>
<td><select name="name1" id="id1"><option value="1">Selection 1</option><option value="2">Selection 2</option></select></td>
</tr>
<tr>
<th><b>Title 2</b></th>
<td><textarea name="name2" id="id2"></textarea></td>
</tr>
<tr>
<th><b>Title 3</b></th>
<td><select name="name3" id="id3"><option value="11">Selection 11</option><option value="12">Selection 12</option></select></td></tr>
<tr>
<td><input type="submit" name="__saveForm" value="Save"></td>
</tr>
</tbody>
</table>
</form>在JS代码之后,很抱歉有不好的约定:
var formwrap = document.createElement("div");
var formform = document.createElement("form");
$.get("some.php", null, function(html) {
var htmlstring = html;
formwrap.innerHTML= htmlstring;
formform = formwrap.querySelector("#thisForm");
document.body.appendChild(formform); // I need to append the form somewhere to the current page, or I get this error in chrome: "Form submission canceled because the form is not connected"
formform.querySelector("#id1").value = '2'; // Set a value in form
formform.querySelector("#id3").value = '12'; // Set a value in form
formform.submit();
}, "text");当代码执行时,我被重定向到包含表单的页面(some.php),但表单实际上并未提交。此外,我通过代码所做的选择在这里也不存在。
有没有办法在浏览器中不打开相应页面(some.php)的情况下提交此表单?
不幸的是,修改表单元素的源代码不是一个选项,我只能更改javascript部分。
谢谢你的帮助
发布于 2020-04-30 22:49:11
我能修好它。
我在Chrome Network Debug中检查到HTTP POST已经发出,但与我手动提交表单时相比,缺少一些参数。我可以通过JS添加这些。
https://stackoverflow.com/questions/61491108
复制相似问题