TypeError:document.getElementById(...)。submit不是函数

时间:2017-04-11 06:25:39

标签: javascript html

我遇到错误,无法找到解决方案而且我不必使用jQuery;我没有提交名称为

的div

我的代码是



<!DOCTYPE html>    
<html>    
<head>    
<script>    
function formSubmit()    
{    
     document.getElementById('web').submit();    
}    
</script>    
</head>    
<body>    
<form action="class003.php" id="g_form" method="POST">    
<input type="text" name="f1" value="">    
</form>    
<div id="web" onclick="formSubmit()">click</div>    
</body>    
</html>
&#13;
&#13;
&#13;

on chrom I&#39; m得到此错误

  

未捕获的TypeError:document.querySelector(...)。submit不是   功能       at formSubmit(test2.html:8)       在HTMLDivElement.onclick(test2.html:19)

Firefox上的

同样的错误

  

TypeError:document.getElementById(...)。submit不是函数[Learn   更多]

4 个答案:

答案 0 :(得分:4)

您需要提交form而不是div

成功

document.getElementById('g_form').submit();    

答案 1 :(得分:3)

您错误输入了元素的id。它应该是g_form

function formSubmit()    
{    
     document.getElementById('g_form').submit();    
} 

答案 2 :(得分:1)

您使用div id 但提交表单您需要指定Form元素的ID

所以只需更改

即可
function formSubmit()    
{    
     document.getElementById('g_form').submit();    // Change the Id here
}

希望它会有所帮助

&#13;
&#13;
<!DOCTYPE html>    
<html>    
<head>    
<script>    
function formSubmit()    
{    
     document.getElementById('g_form').submit();    // Change the Id here
}    
</script>    
</head>    
<body>    
<form action="class003.php" id="g_form" method="POST">    
<input type="text" name="f1" value="">    
</form>    
<div id="web" onclick="formSubmit()">click</div>    
</body>    
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

这是您的解决方案。

<!DOCTYPE html>    
<html>    
<head>    
<script>    
function formSubmit()    
{    
     document.forms['g_form'].submit();
}    
</script>    
</head>    
<body>      

<form action="class003.php" id="g_form" name="g_form" method="POST">
    <input type="text" name="f1" value=""> 

</form>
 <div class="button1" onClick="formSubmit();">Click</div>
</body>    
</html>
相关问题