Submit form using tag

前端 未结 11 2070
滥情空心
滥情空心 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:26

    Here is how I would do it using vanilla JS

    <form id="myform" method="POST" action="xxx">
        <!-- your stuff here -->
        <a href="javascript:void()" onclick="document.getElementById('myform').submit();>Ponies await!</a>
    </form>
    

    You can play with the return falses and href="#" vs void and whatever you need to but this method worked for me in Chrome 18, IE 9 and Firefox 14 and the rest depends on your javascript mostly.

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

    Is there any reason for not using a submit button and then styling it with css to match your other a tags?

    I think this would reduce your dependency on having javascript enabled and still get the desired result.

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

    Try this:

    Suppose HTML like this :

       <form id="myform" name="myform" method="POST" action="process_edit_questionnaire.php?project=<?php echo $project_id; ?>">
          <div id="question_block">
                testing form
            </div>
         <a href="javascript: submit();">Submit</a>
            </form>
    

    JS :

       <script type='text/javascript'>
         function submit()
          {
             document.forms["myform"].submit();
          }
       </script>
    

    you can check it out here : http://jsfiddle.net/Zm426/7/

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

    Clean and simple:

      <form action="/myaction" method="post" target="_blank">
        <!-- other elements -->
        <a href="#" onclick="this.parentNode.submit();">Submit</a>
     </form>
    

    In case of opening form action url in a new tab (target="_blank"):

      <form action="/myaction" method="post" target="_blank">
        <!-- other elements -->
        <a href="#" onclick="this.parentNode.submit();">Submit</a>
     </form>
    
    0 讨论(0)
  • 2020-12-13 00:34

    Using Jquery you can do something like this:

    $(document).ready(function() {
      $('#btnSubmit').click(function() {
        $('#deleteFrm').submit();
      });
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form action="" id="deleteFrm" method="POST">
      <a id="btnSubmit">Submit</a>
    </form>

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

    You can use hidden submit button and click it using java script/jquery like this:

      <form id="contactForm" method="post" class="contact-form">
    
            <button type="submit" id="submitBtn" style="display:none;" data-validate="contact-form">Hidden Button</button>
            <a href="javascript:;" class="myClass" onclick="$('#submitBtn').click();">Submit</a>
    
      </form>
    
    0 讨论(0)
提交回复
热议问题