Can you have a single logical <form> split across multiple
s?

后端 未结 4 591
Happy的楠姐
Happy的楠姐 2021-01-01 18:34

In my existing web page layout, which involves multiple files, one specifying the high-level layout, there are two elements, one for a left column, and one for a right colu

相关标签:
4条回答
  • 2021-01-01 18:59

    (Sorry to unaccept an answer and post my own, but I just found out apparently the answers I got are not up to date with HTML5!)

    HTML5 introduces a form= attribute for <input> element [1]. So that you can do <input form="form1" name="input1" /> and have it in a nested inner form, or even outside of the` element.

    [1] http://www.w3.org/TR/html5/association-of-controls-and-forms.html#attr-fae-form

    0 讨论(0)
  • 2021-01-01 19:00

    Short answer is No, you cannot have two separate form tags acting as a 1 form, instead wrap the whole thing inside a wrapping div tag

    <form>
      <!-- some content with input elements has no relation with other form tag on this page, it just ends where you specify the end tag -->
    </form>
    
    <form>
      <!-- Has  no relation with the first form -->
    </form>
    
    0 讨论(0)
  • 2021-01-01 19:07

    The only way to submit both forms is to wrap both <div>s within a single <form> tag. Or, you can use jQuery/JavaScript to aggregate the data and submit them together as one submission.

    0 讨论(0)
  • 2021-01-01 19:08

    I think people are missing the question. A <form> can contain whatever elements it needs. If the layout of your page is something like:

    <body>
        <div id="leftColumn"><input name="foo" type="text"></div>
        <div id="rightColumn"><input name="bar" type="text"></div>
    <body>
    

    You don't need separate foo and bar forms. You can happily encase both of those <div>s inside a single <form> element:

    <body>
        <form name="onlyOneForm">
            <div id="leftColumn"><input name="foo" type="text"></div>
            <div id="rightColumn"><input name="bar" type="text"></div>
        </form>
    <body>
    
    0 讨论(0)
提交回复
热议问题