我有以下简单的 HTML 表单,我试图在页面加载期间自动提交表单。 下面的 Javascript 代码不会自动提交表单。
HTML :
<form action="SSL.php" method="POST" name="TForm" id="transactionForm">
<input type="hidden" name="merchantTxnId" id="merchantTxnId" value="test">
<input type="submit" name="submit" id="submit" value="submit" style="visibility:hidden">
</form>
Redirecting ... Please wait...
Java 脚本:
<script>
window.onload = function(){
alert(1); // this is working..
document.getElementById("transactionForm").submit(); //nothing is happening with this line . form is not getting submitted
}
</script>
我在 Chrome 控制台模式下发现以下错误: 请告诉我问题出在哪里...
最佳答案
您不得将 submit
用作任何表单元素的 name
或 id
。
原因是,您可以通过 document.getElementById('form').nameOfTheChild
访问表单的每个子项,其中 nameOfTheChild
是 name
的 child 。如果您有一个名为 submit
的 child ,document.getElementById('form').submit
是解决该 child 的快捷方式。
.submit()
的文档说:
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.
关于Javascript 自动表单提交不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843175/