如何使用javascript提交表单?

Nat*_*ate 165 javascript

我有一个id为id的表单,theForm其中包含以下div,其中包含一个提交按钮:

<div id="placeOrder" 
     style="text-align: right; width: 100%; background-color: white;">
     <button type="submit" 
             class='input_submit' 
             style="margin-right: 15px;" 
             onClick="placeOrder()">Place Order
     </button>
</div>
Run Code Online (Sandbox Code Playgroud)

单击时,将placeOrder()调用该函数.该函数将上面div的innerHTML更改为"processing ..."(因此提交按钮现在消失了).

上面的代码有效,但现在的问题是我无法提交表单!我试过把它放在placeOrder()函数中:

document.theForm.submit();
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

我怎样才能提交表格?

And*_*are 123

name表单的属性设置为"theForm",您的代码将起作用.

  • 这对某些人来说可能看起来非常明显,但我有一个名称和id为"submit"的按钮,而`document.theForm.submit()`报告错误.一旦我重命名了我的按钮,代码就完美无缺. (11认同)

ale*_*lex 93

您可以使用...

document.getElementById('theForm').submit();
Run Code Online (Sandbox Code Playgroud)

...但不要取代innerHTML.您可以隐藏表单,然后插入一个处理... span显示在其位置.

var form = document.getElementById('theForm');

form.style.display = 'none';

var processing = document.createElement('span');

processing.appendChild(document.createTextNode('processing ...'));

form.parentNode.insertBefore(processing, form);
Run Code Online (Sandbox Code Playgroud)

  • 因为我的表单名称是 printoffersForm 并且 id 是相同的。`document.getElementById('printoffersForm').submit();` 但我给了我错误 submit is not an function (2认同)

Chi*_*mar 32

document.getElementById("form1").submit();
Run Code Online (Sandbox Code Playgroud)

在我的情况下它完美无缺.

你也可以在功能上使用它,比如

function formSubmit()
{
     document.getElementById("form1").submit();
} 
Run Code Online (Sandbox Code Playgroud)

  • @ChintanThummar然后你可以使用document.getElementById("form1")处理[表单提交](http://mycodingtricks.com/javascript/submit-form-using-javascript-ajax/).onsubmit = function(){ //您可以在此处使用Ajax代码提交表单//无需刷新页面} (2认同)

小智 14

<html>

    <body>

        <p>Enter some text in the fields below, and then press the "Submit form" button to submit the form.</p>

        <form id="myForm" action="/action_page.php">
          First name: <input type="text" name="fname"><br>
          Last name: <input type="text" name="lname"><br><br>
          <input type="button" onclick="myFunction()" value="Submit form">
        </form>

        <script>
            function myFunction() {
                document.getElementById("myForm").submit();
            }
        </script>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)


小智 10

document.forms["name of your form"].submit();
Run Code Online (Sandbox Code Playgroud)

要么

document.getElementById("form id").submit();
Run Code Online (Sandbox Code Playgroud)

你可以尝试任何这个...这将是肯定的工作..


小智 9

我将离开我提交表单的方式,而不使用表单中的name标签:

HTML

<button type="submit" onClick="placeOrder(this.form)">Place Order</button>
Run Code Online (Sandbox Code Playgroud)

JavaScript

function placeOrder(form){
    form.submit();
}
Run Code Online (Sandbox Code Playgroud)


小智 5

HTML

<!-- change id attribute to name  -->

<form method="post" action="yourUrl" name="theForm">
    <button onclick="placeOrder()">Place Order</button>
</form>
Run Code Online (Sandbox Code Playgroud)

JavaScript

function placeOrder () {
    document.theForm.submit()
}
Run Code Online (Sandbox Code Playgroud)

  • 请在您的答案中添加一些解释。 (4认同)

Abh*_*ire 5

您可以使用以下代码通过 JavaScript 提交表单:

document.getElementById('FormID').submit();
Run Code Online (Sandbox Code Playgroud)