Submit form using a button outside the <form> tag

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

Say I have:

13条回答
  •  北恋
    北恋 (楼主)
    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:

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

    
    

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

提交回复
热议问题