Can I nest form tags in other form tags?

2019-01-09 12:18发布

问题:

Can I put a <form> tag inside another <form> tag?

For example:

<form>
  <form>
  </form>
</form>

回答1:

No, nested forms are forbidden.


This is expressed in the HTML 4.01 DTDs as:

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

— http://www.w3.org/TR/html4/interact/forms.html#h-17.3

This means A FORM has a mandatory start tag, mandatory end tag and can contain anything in %block or SCRIPT, except other FORMs.


XML DTDs aren't as expressive as SGML DTDs so in XHTML this rule is specified only in the human readable text of the specification:

form must not contain other form elements.

— http://www.w3.org/TR/xhtml1/#prohibitions


HTML 5 isn't an SGML application and doesn't have an official machine readable description of the language. It also expresses this rule in text:

Content model:

Flow content, but with no form element descendants.

— http://www.w3.org/TR/html5/forms.html#the-form-element



回答2:

You can't nest them, but you can group elements...there's a mechanism specifically for this, the <fieldset> element, used to group controls/labels...and in HTML5 associate it to specific forms, disable contents, etc.



回答3:

Nested forms are not allowed per the specification (I'm not sure what the behavior would be when attempted, I haven't tried it).

There's some interesting discussion of the topic in a previous question, however.



回答4:

No, use <fieldset> if you want to split your forms. Some browsers might be able to parse it (didn't tested it) but according to the w3 standard it is not allowed to use forms within other forms.



回答5:

Works fine with XHTML 1.0 Strict.

I even tested validation by pasting the below into http://validator.w3.org/check.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Markup Test</title>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
  </head>
  <body>
    <h1>Markup Test</h1>
    <p>
      This doc contains a form with a nested form and validates at
      <a title="Validate using W3C Markup Validation Service" href="http://validator.w3.org/check?uri=referer">
        http://validator.w3.org/
      </a>
    </p>
    <form action="#">
      <div>
        <form action="#">
        </form>
      </div>
    </form>
  </body>
</html>

HOWEVER Quentin below is correct: I tested and the nested form opening and closing tags are ignored. Form input elements appearing before, within, and after the child form tags are treated as members of the parent form, including submit buttons. JavaScript references to this.form from within the child form reference the parent form. If you give the child form an id and retrieve it via getElementById then you get an element but .submit() does nothing. Its basically one form (the parent) with garbage tags in it.



回答6:

Directly within the Forms are not allowed. All your javascript will not work and intended behaviour will not be there.

However, you can try this:

<form name ="1st form">
<...>
<div> <form id="dummy"></form></div>
<...>
<form name = "2nd form">
</form>
<...>
</form>

You will need to create a dummy form in between the 2 forms you intended to use. 1 note of caution, 1st Form and the dummy form will need to have some tags in between them. otherwise this will not work.



标签: html forms