Confuse about ( Form attribute )

2019-06-21 08:17发布

I'm confused about using the form attribute (Form=" ")

From MDN

An input can be associated with at most one form.

From W3S

The form attribute specifies one or more forms an element belongs to.
To refer to more than one form, use a space-separated list of form ids.

So,Can i associate an input with more than one form ? or not ?
Which one is true ?

2条回答
虎瘦雄心在
2楼-- · 2019-06-21 08:33

There is no definite affirmative answer. It seams that we can only say that, since nowhere in the w3 consortium specs it is stated that an input can be associated with multiple forms, it implies that it cannot; (and also a conclusion that w3schools info should be taken with a grain of salt as CBroe noted).

When I tested it - using two forms with ids "form1" and "form2", and an 'outside' input element with form-attribute set to form="form1 form2" - this input did not submit with neither of forms. Meaning that, since form-attribute is not exactly set to one form id, it doesn't connect it to any form.

Further in the test I have added a third form with form id="form1 form2". This resulted in input element (which is outside of all of the forms, with form-attribute set to form="form1 form2") to get submitted with this form, but not with form with id="form1" nor with form with id="form2".

Therefore, with my limited knowledge, I dare to make a conclusion:

One input element cannot be associated with multiple forms by specifying space-delimited ids for its form-attribute (or by any other means most likely).

This question is a duplicate of Multiple form ID's in HTML5's input form attribute, but there is a bit more detailed explanation here.

查看更多
可以哭但决不认输i
3楼-- · 2019-06-21 08:45

No.. The from attribute did not exist prior to HTML5's release. You will need to look at your DOCTYPE and make that determination.

Before HTML5, you could use Javascript and do some stuff with Xhr to accomplish that type of behavior if it was actually needed however.

I found the example you cited...

Last name: <input type="text" name="lname" form="form1">

Here is the description in HTML5 Form Specs: https://www.w3.org/TR/html5/sec-forms.html#element-attrdef-formelements-form

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

If a reassociateable form-associated element has a form attribute specified, then that attribute’s value must be the ID of a form element in the element’s owner Document.

Quite a few other comments in the RFC. Recommend taking a look there.

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

Updated after more consideration not seeing anything which suggests a form attribute can specify more than one form. Most concise text from W3C spec to support that would be: "form - Associates the control with a form element".

查看更多
登录 后发表回答