javascript - 未捕获的 TypeError : document. getElementById(...).submit 不是 HTMLDocument 中的函数。<anonymous>

标签 javascript jquery html

我想通过 jquery/javascript 提交表单,为此我使用 javascript (document.getElementById('formId').submit(); ) 但我们得到错误:

        file.php:144 Uncaught TypeError: document.getElementById(...).submit is not a function
            at HTMLDocument.<anonymous> (file.php:144)
            at fire (jquery.js:3048)
            at Object.fireWith [as resolveWith] (jquery.js:3160)
            at Function.ready (jquery.js:433)
            at HTMLDocument.completed (jquery.js:104)

我发现如果我在表单中使用 HTML 标签,则会出现错误 HTML 标签如:

<ul class="pager">
  <li class="next" >

如果我以过度形式删除这些标签,那么它就可以正常工作

     <form action=""  name="formId" id="formId" method="post" >
              <fieldset  class='panel ques'  style='margin:5%;' id='ques_1' >
                Question &nbsp;1&nbsp;:: what is command for changing user information??</b><br /><br />
                <input type="radio" name="a" value="a" checked>
                <label for="55892169d2efc"> usermod</label><br /><br />
                <input type="radio" name="b" value="b">
                <label for="55892169d2f05"> useradd</label><br /><br />
                <input type="radio"  name="c" value="c">
                <label for="55892169d2f09"> useralter</label><br /><br />
                <input type="radio"  name="d" value="d">
                <label for="55892169d2f0c"> groupmod</label><br /><br />
                <ul class="pager">
                  <li class="next" >
                    <button type="text" style="float: right;" name="submit" class="btn btn-primary">
                      <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>&nbsp;Finish
                    </button>
                  </li>
                </ul>
              </fieldset>
            </form>

    <script type="text/javascript">
                   document.getElementById('formId').submit();  
                 </script>

最佳答案

您将提交按钮命名为 submit,它覆盖了 formsubmit() 方法。重命名按钮可以解决该问题。

因此,作为一般最佳实践,不要为事物命名或提供与对象、属性或方法名称相匹配的 ID。

<form action="http://example.com" name="formId" id="formId" method="post" >
              <fieldset  class='panel ques'  style='margin:5%;' id='ques_1' >
                Question &nbsp;1&nbsp;:: what is command for changing user information??<br><br>
                <input type="radio" name="a" value="a" checked>
                <label for="55892169d2efc"> usermod</label><br><br>
                <input type="radio" name="b" value="b">
                <label for="55892169d2f05"> useradd</label><br><br>
                <input type="radio"  name="c" value="c">
                <label for="55892169d2f09"> useralter</label><br><br>
                <input type="radio"  name="d" value="d">
                <label for="55892169d2f0c"> groupmod</label><br><br>
                <ul class="pager">
                  <li class="next" >
                    <button type="text" style="float: right;" name="btnSubmit" class="btn btn-primary">
                      <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>&nbsp;Finish
                    </button>
                  </li>
                </ul>
              </fieldset>
            </form>

<script>
  document.getElementById('formId').submit();  
</script>

关于javascript - 未捕获的 TypeError : document. getElementById(...).submit 不是 HTMLDocument 中的函数。<anonymous>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260673/

相关文章:

javascript - 清除前一个值后保护文本框不取该空白值

javascript - 在 HTML5 Ipad 应用程序中捕获滑动效果以导航到下一页

javascript - 两个具有相同类名的 jquery slider ,但问题在于移动 slider (2)

javascript - 如何在 HTML 框中放置输入框(表单)并使用 JQuery 定位它

javascript - 将浏览器更新脚本集成到 .js 文件中的正确语法是什么?

javascript - 为什么函数的调用成员不能赋值给变量

javascript - 取 $elements 变量并缩小 attr 等于 x

javascript - 如何替换链接名称文本

javascript - 在 Java 脚本函数警报框中重定向页面(确定)单击

javascript - AngularJS 范围监视未触发