Submit form using a button outside the <form> tag

前端 未结 13 1225
南旧
南旧 2020-11-22 09:45

Say I have:

相关标签:
13条回答
  • 2020-11-22 10:12

    Try this:

    <input type="submit" onclick="document.forms[0].submit();" />
    

    Although I would suggest adding an id to the form and accessing by that instead of document.forms[index].

    0 讨论(0)
  • 2020-11-22 10:12

    I had an issue where I was trying to hide the form from a table cell element, but still show the forms submit-button. The problem was that the form element was still taking up an extra blank space, making the format of my table cell look weird. The display:none and visibility:hidden attributes didn't work because it would hide the submit button as well, since it was contained within the form I was trying to hide. The simple answer was to set the forms height to barely nothing using CSS

    So,

    CSS -

        #formID {height:4px;}
    

    worked for me.

    0 讨论(0)
  • 2020-11-22 10:16

    Here's a pretty solid solution that incorporates the best ideas so far as well as includes my solution to a problem highlighted with Offerein's. No javascript is used.

    If you care about backwards compatibility with IE (and even Edge 13), you can't use the form="your-form" attribute.

    Use a standard submit input with display none and add a label for it outside the form:

    <form id="your-form">
      <input type="submit" id="your-form-submit" style="display: none;">
    </form>
    

    Note the use of display: none;. This is intentional. Using bootstrap's .hidden class conflicts with jQuery's .show() and .hide(), and has since been deprecated in Bootstrap 4.

    Now simply add a label for your submit, (styled for bootstrap):

    <label for="your-form-submit" role="button" class="btn btn-primary" tabindex="0">
      Submit
    </label>
    

    Unlike other solutions, I'm also using tabindex - set to 0 - which means that we are now compatible with keyboard tabbing. Adding the role="button" attribute gives it the CSS style cursor: pointer. Et voila. (See this fiddle).

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

    In HTML5, there is the form attribute. Basically

    <form id="myform" method="get" action="something.php">
        <input type="text" name="name" />
    </form>
    
    <input type="submit" form="myform" />
    
    0 讨论(0)
  • 2020-11-22 10:25

    Similar to another solution here, with minor modification:

    <form method="METHOD" id="FORMID">
       <!-- ...your inputs -->
    </form>
    <button type="submit" form="FORMID" value="Submit">Submit</button>
    

    https://www.w3schools.com/tags/att_form.asp

    0 讨论(0)
  • 2020-11-22 10:26

    if you can use jQuery you can use this

    <form method="get" action="something.php" id="myForm">
        <input type="text" name="name" />
    
        <input type="submit" style="display:none" />
    </form>
    
    <input type="button" value="Submit" id="myButton" />
    
    <script type="text/javascript">
        $(document).ready(function() {
           $("#myButton").click(function() {
               $("#myForm").submit();
           });
        });
    </script>
    

    So, the bottom line is to create a button like Submit, and put the real submit button in the form(of course hiding it), and submit form by jquery via clicking the 'Fake Submit' button. Hope it helps.

    0 讨论(0)
提交回复
热议问题