How can I submit a form using JavaScript?

后端 未结 10 1623
北海茫月
北海茫月 2020-11-22 16:43

I have a form with id theForm which has the following div with a submit button inside:

相关标签:
10条回答
  • 2020-11-22 17:05

    If your form does not have any id, but it has a class name like theForm, you can use the below statement to submit it:

    document.getElementsByClassName("theForm")[0].submit();
    
    0 讨论(0)
  • 2020-11-22 17:06

    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()
    }
    
    0 讨论(0)
  • 2020-11-22 17:13

    Set the name attribute of your form to "theForm" and your code will work.

    0 讨论(0)
  • 2020-11-22 17:18

    I will leave the way I do to submit the form without using the name tag inside the form:

    HTML

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

    JavaScript

    function placeOrder(form){
        form.submit();
    }
    
    0 讨论(0)
提交回复
热议问题