Can you nest html forms?

前端 未结 20 3109
悲&欢浪女
悲&欢浪女 2020-11-21 04:56

Is it possible to nest html forms like this

so

相关标签:
20条回答
  • 2020-11-21 05:20

    No you cannot have a nested form. Instead you can open up a Modal that contains form and perform Ajax form submit.

    0 讨论(0)
  • 2020-11-21 05:23

    Even if you could get it to work in one browser, there's no guarantee that it would work the same in all browsers. So while you might be able to get it to work some of the time, you certainly wouldn't be able to get it to work all of the time.

    0 讨论(0)
  • 2020-11-21 05:23

    About nesting forms: I spent 10 years one afternoon trying to debug an ajax script.

    my previous answer/example didn't account for the html markup, sorry.

    <form id='form_1' et al>
      <input stuff>
      <submit onClick='ajaxFunction(That_Puts_form_2_In_The_ajaxContainer)' >
      <td id='ajaxContainer></td>
    </form>
    

    form_2 constantly failed saying invalid form_2.

    When I moved the ajaxContainer that produced form_2 <i>outside</i> of form_1, I was back in business. It the answer the question as to why one might nest forms. I mean, really, what's the ID for if not to define which form is to be used? There must be a better, slicker work around.

    0 讨论(0)
  • 2020-11-21 05:27

    As it's 2019 I'd like to give an updated answer to this question. It is possible to achieve the same result as nested forms, but without nesting them. HTML5 introduced the form attribute. You can add the form attribute to form controls outside of a form to link them to a specific form element (by id).

    https://www.impressivewebs.com/html5-form-attribute/

    This way you can structure your html like this:

    <form id="main-form" action="/main-action" method="post"></form>
    <form id="sub-form"  action="/sub-action"  method="post"></form>
    
    <div class="main-component">
        <input type="text" name="main-property1" form="main-form" />
        <input type="text" name="main-property2" form="main-form" />
    
        <div class="sub-component">
            <input type="text" name="sub-property1" form="sub-form" />
            <input type="text" name="sub-property2" form="sub-form" />
            <input type="submit" name="sub-save" value="Save" form="sub-form" />
        </div>
    
        <input type="submit" name="main-save" value="Save" form="main-form" />
    </div>
    

    The form attribute is supported by all modern browsers. IE does not support this though but IE is not a browser anymore, rather a compatibility tool, as confirmed by Microsoft itself: https://www.zdnet.com/article/microsoft-security-chief-ie-is-not-a-browser-so-stop-using-it-as-your-default/. It's about time we stop caring about making things work in IE.

    https://caniuse.com/#feat=form-attribute
    https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form

    From the html spec:

    This feature allows authors to work around the lack of support for nested form elements.

    0 讨论(0)
  • 2020-11-21 05:27

    Before I knew I wasn't supposed to do this I had nested forms for the purpose of having multiple submit buttons. Ran that way for 18 months, thousands of signup transactions, no one called us about any difficulties.

    Nested forms gave me an ID to parse for the correct action to take. Didn't break 'til I tried to attach a field to one of the buttons and Validate complained. Wasn't a big deal to untangle it--I used an explicit stringify on the outer form so it didn't matter the submit and form didn't match. Yeah, yeah, should've taken the buttons from a submit to an onclick.

    Point is there are circumstances where it's not entirely broken. But "not entirely broken" is perhaps too low a standard to shoot for :-)

    0 讨论(0)
  • 2020-11-21 05:29

    Note you are not allowed to nest FORM elements!

    http://www.w3.org/MarkUp/html3/forms.html

    https://www.w3.org/TR/html4/appendix/changes.html#h-A.3.9 (html4 specification notes no changes regarding nesting forms from 3.2 to 4)

    https://www.w3.org/TR/html4/appendix/changes.html#h-A.1.1.12 (html4 specification notes no changes regarding nesting forms from 4.0 to 4.1)

    https://www.w3.org/TR/html5-diff/ (html5 specification notes no changes regarding nesting forms from 4 to 5)

    https://www.w3.org/TR/html5/forms.html#association-of-controls-and-forms comments to "This feature allows authors to work around the lack of support for nested form elements.", but does not cite where this is specified, I think they are assuming that we should assume that it's specified in the html3 specification :)

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