Is <input> well formed without a <form>

2019-01-05 10:04发布

Is it valid to have <input> without it being in a <form>?

Have a process for marking up some fields in pages and just found a page with input fields that where not being marked up as I expected.
It's taken me a while but worked out that the process of getting the form elements then getting the fields is what caused these to be missed because there is no form.

标签: html forms input
6条回答
来,给爷笑一个
2楼-- · 2019-01-05 10:04

Yes, you can have a valid input without a form.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-05 10:06

I checked the following with the W3C validator and it confirms this is valid.

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
</head>
<body>
  <input type='text' />
</body>
</html>
查看更多
Juvenile、少年°
4楼-- · 2019-01-05 10:07

According to MDN it is possible:

Note that it's always possible to use a form widget outside of a element but if you do so, that form widget has nothing to do with any form. Such widgets can be used outside a form, but then you should have a special plan for such widgets, since they will do nothing on their own. You will have to customize their behavior with JavaScript.

HTML5 introduces the form attribute on HTML form elements. It should let you explicitly bind an element with a form even if it is not enclosed within a . Unfortunately, for the time being, the implementation of this feature across browsers is not yet good enough to rely on it.

查看更多
ら.Afraid
5楼-- · 2019-01-05 10:10

<input> without a <form> appears valid, yes (at least for html 4.01, look near the end of 17.2.1):

The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on intrinsic events. Note that controls outside a form cannot be successful controls.

查看更多
爷、活的狠高调
6楼-- · 2019-01-05 10:15

Reference to a more up-to-date specification:

HTML 5.2 - W3C Recommendation (14 December 2017)

A form-associated element can have a relationship with a <form> element, which is called the element’s form owner. If a form-associated element is not associated with a <form> element, its form owner is said to be null.

查看更多
做个烂人
7楼-- · 2019-01-05 10:30

Sure, use

document.getElementById('your_input_id').validity.valid

to check validity of field dynamically.

查看更多
登录 后发表回答