我有一个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>

单击时,将调用placeOrder()函数。该函数将上述div的innerHTML更改为“processing…”(所以提交按钮现在没有了)。

上面的代码工作,但现在的问题是,我不能让表单提交!我试着把这个放在placeOrder()函数中:

document.theForm.submit();

但这行不通。

我怎样才能提交表单?


当前回答

<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>

其他回答

HTML

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

<form method="post" action="yourUrl" name="theForm">
    <button onclick="placeOrder()">Place Order</button>
</form>

JavaScript

function placeOrder () {
    document.theForm.submit()
}

我将保留我提交表单的方式,而不使用表单内部的名称标签:

HTML

<button type="submit" onClick="placeOrder(this.form)">Place Order</button>

JavaScript

function placeOrder(form){
    form.submit();
}

你可以使用下面的代码使用JavaScript提交表单:

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

你可以用…

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

...但是不要替换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);

如果你的表单没有任何id,但是它有一个类似theForm的类名,你可以使用下面的语句来提交它:

document.getElementsByClassName("theForm")[0].submit();