Submit form using tag

前端 未结 11 2071
滥情空心
滥情空心 2020-12-12 23:55

I am trying to submit a form through onclick event on tag. I have tried triggering document.myform.submit(), this.form.submit(), parentNode.submit() etc. but none of this i

相关标签:
11条回答
  • 2020-12-13 00:39
    <form  id="myform_id" action="/myMethode"  role="form"  method="post" > 
       <a  href="javascript:$('#myform_id').submit();" >submit</a>
    </form>
    
    0 讨论(0)
  • 2020-12-13 00:42

    If jquery is allowed then you can use following code to implement it in the easiest way as :

    <a href="javascript:$('#form_id').submit();">Login</a>
    

    or

    <a href="javascript:$('form').submit()">Login</a>
    

    You can use following line in your head tag () to import jquery into your code

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    
    0 讨论(0)
  • 2020-12-13 00:43

    I suggest to use "return false" instead of useing some javascript in the href-tag:

    <form id="">
    ...
    </form>
    
    <a href="#" onclick="document.getElementById('myform').submit(); return false;">send form</a>
    
    0 讨论(0)
  • 2020-12-13 00:46

    you can do it like this with raw javascript

    <html>
        <body>
            <form id="my_form" method="post" action="mailto://test@test.com">
                <a href="javascript:{}" onclick="document.getElementById('my_form').submit();">submit</a>
            </form>
        </body>
    </html>
    

    For those asking why I have a href element in my anchor tag, its because it's a compulsory part of an anchor tag, it may well work without but it's not to spec. So if you want to guarantee rendering etc you must give the engine a fully formed tag. So the above achieves this. You will see href="#" used sometimes but this is not always wanted as the browser will change the page position.

    0 讨论(0)
  • 2020-12-13 00:51

    Submit your form like this ...

    Your HTML code

        <form name="myform" action="handle-data.php"> Search: <input type='text' name='query' />
         <a href="javascript: submitform()">Search</a> 
       </form> 
    

    JavaScript Code

      <script type="text/javascript"> 
            function submitform() {   document.myform.submit(); } 
       </script> 
    
    0 讨论(0)
提交回复
热议问题