JavaScript 如何解决“Submit is not a function”错误
您是否尝试使用JavaScript提交表单?但是,当您尝试使用JavaScript提交表单时,可能会在代码中遇到“Submit is not a function”错误。好吧,暂时不要慌张。本文将致力于解决这个问题。
那么我们还在等什么呢?让我们深入研究吧。
示例: 在这里您会遇到“Submit is not a function”错误。
<!DOCTYPE html>
<html>
<head>
<title>“Submit is not a function”
error in JavaScript</title>
</head>
<body>
<body style="text-align:center;">
<h2 style="color:green">GeeksForGeeks</h2>
<h2 style="color:purple">
“Submit is not a function” error
</h2>
<form action="product.php"
method="get"
name="frmProduct"
id="frmProduct"
enctype="multipart/form-data">
<input onclick="submitAction()"
id="submit_value"
type="button"
name="submit_value"
value="CLICK HERE">
</form>
<script type="text/javascript">
function submitAction() {
document.frmProduct.submit();
}
</script>
</body>
<html>
错误:
(图片之后文章继续)
这个问题有5种不同的解决方案。
解决方案1:
只需将你的按钮名称更改为 btnSubmit 或其他任何名称。你的代码将奇迹般地工作。这是因为你已经在这个代码中给提交按钮或其他元素命名为 submit 当按钮被命名为submit时,它会覆盖此代码上的submit()函数。
btnSubmit
示例:
<!DOCTYPE html>
<html>
<head>
<title>
“Submit is not a function” error in JavaScript
</title>
</head>
<body style="text-align:center;">
<h2 style="color:green">GeeksForGeeks</h2>
<h2 style="color:purple">
“Submit is not a function” error
</h2>
<form action="product.php"
method="get"
name="frmProduct"
id="frmProduct"
enctype="multipart/form-data">
<input onclick="submitAction()"
id="submit_value"
type="button"
name="submit_value"
value="CLICK HERE">
</form>
<script type="text/javascript">
function submitAction() {
document.frmProduct.btnSubmit();
}
</script>
</body>
<html>
解决方案2:
只需让按钮处理并决定使用表单中的哪个对象。
onclick="return SubmitForm(this.form)"
示例:
<!DOCTYPE html>
<html>
<head>
<title>
“Submit is not a function”
error in JavaScript
</title>
</head>
<body>
<body style="text-align:center;">
<h2 style="color:green">
GeeksForGeeks
</h2>
<h2 style="color:purple">
“Submit is not a function” error
</h2>
<form action="product.php"
method="get"
name="frmProduct"
id="frmProduct"
enctype="multipart/form-data">
<input onclick="return SubmitForm(this.form)"
id="submit_value"
type="button"
name="submit_value"
value="CLICK HERE">
</form>
<script type="text/javascript">
function submitAction() {
document.frmProduct.submit();
}
</script>
</body>
<html>
解决方案3:
如果表单中没有 name=”submit” 或 id=”submit” ,请确保删除或编辑它。同时确保没有其他表单具有相同的名称。否则会报错。
解决方案4:
如果通过实施解决方案3(即有机会更改 name=”submit” 或 id=”submit” )后,输出没有变化(仍然出现错误),您还可以通过 更改JavaScript代码来避免错误。
示例:
<!DOCTYPE html>
<html>
<head>
<title>
“Submit is not a
function” error in JavaScript
</title>
</head>
<body style="text-align:center;">
<h2 style="color:green">
GeeksForGeeks
</h2>
<h2 style="color:purple">
“Submit is not a function” error
</h2>
<form action="product.php"
method="get"
name="frmProduct"
id="frmProduct"
enctype="multipart/form-data">
<input onclick="submitAction()"
id="submit_value"
type="button"
name="submit_value"
value="CLICK HERE">
</form>
<script type="text/javascript">
function submitForm(form) {
var submitFormFunction =
Object.getPrototypeOf(form).submit;
submitFormFunction.call(form);
}
</script>
</body>
<html>
解决方案5: 通过对JavaScript进行一些修改。
示例:
<!DOCTYPE html>
<html>
<head>
<title>
“Submit is not a function”
error in JavaScript
</title>
</head>
<body style="text-align:center;">
<h2 style="color:green">
GeeksForGeeks
</h2>
<h2 style="color:purple">
“Submit is not a function” error
</h2>
<form action="product.php"
method="get"
name="frmProduct"
id="frmProduct"
enctype="multipart/form-data">
<input onclick="submitAction()"
id="submit_value"
type="button"
name="submit_value"
value="CLICK HERE">
</form>
<script type="text/javascript">
function submitForm(form) {
var enviar =
document.getElementById("enviar");
enviar.type = "submit";
}
</script>
</body>
<html>
输出:
当我们编写这段代码时
错误消息如下:
使用解决方案后: s
“提交”不是一个函数的错误