Javascript 自动表单提交不起作用

标签 javascript html forms

我有以下简单的 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 控制台模式下发现以下错误: enter image description here 请告诉我问题出在哪里...

最佳答案

您不得将 submit 用作任何表单元素的 nameid

原因是,您可以通过 document.getElementById('form').nameOfTheChild 访问表单的每个子项,其中 nameOfTheChildname 的 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/

相关文章:

javascript - 为什么Meteor把js全部放在开头呢?

javascript - Material-UI 工具提示需要删除子标题 Prop

javascript - 当有人点击链接(图像)时,是否可以缩放?

jquery - 如何使用 jquery 设置两个 div 的百分比宽度动画

Django:使用实例初始化自定义表单的 FormSet

javascript - string.replace() 与 global (g) 不以奇怪的方式工作

javascript 附加子项

javascript - 如何获取点击元素的索引?

php - 如何在 MySQL 中存储数据的 php 表单中保留 HTML 样式?

javascript - HTML5 验证的表单事件失败?