当输入名称称为Submit时,为什么表单不使用JavaScript提交? [英] Why the form not submitting using JavaScript when input name called submit?
问题描述
我有以下表格.当我将输入名称设置为 submit
时,它不会提交表单,为什么?
I have the following form. When I have the input name set to submit
, it won't submit the form, why is that?
<form id="login" method="post" action="">
<input type="hidden" name="username" value="someval">
<input type="hidden" name="userpass" value="somepass">
<input type="hidden" name="submit" value="Login">
</form>
<script type="text/javascript">
var timeout = 12;
setTimeout(function () {
document.getElementById('login').submit();
}, timeout);
</script>
错误消息是:
未捕获的TypeError:document.getElementById(...).submit不是函数(...)
Uncaught TypeError: document.getElementById(...).submit is not a function(…)
我认为提交名称正在以某种方式擦除提交功能?
I guess the submit name is erasing the submit function in some way?
JSFiddle with and without submit name.
当包含名为Submit的输入时,如何使用js提交表单?
How can I submit the form using js when it includes input named submit?
推荐答案
因为您可以将表单的子元素作为直接属性来访问.因此,在您的情况下:
Because you may access child elements of the form as direct properties. So in your case:
'use strict';
let form = document.getElementById('login');
console.log(form.username, form.userpass, form.submit); // 3 input elements
因此,当您有一个名为 submit
的元素时,它会遮盖 submit()
函数,因此您会收到有关其不是函数的错误.
So when you have an element named submit
, it shadows the submit()
function, and thus you get the error about it not being a function.
这篇关于当输入名称称为Submit时,为什么表单不使用JavaScript提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!