有<input>而不是<form>是有效的吗?
有一个在页面中标记一些字段的过程,只是发现了一个输入字段的页面,那里没有被标记为我所期望的。 这花了我一段时间,但得出的过程中,得到的形式元素,然后得到的领域是什么导致这些错过,因为没有形式。
有<input>而不是<form>是有效的吗?
有一个在页面中标记一些字段的过程,只是发现了一个输入字段的页面,那里没有被标记为我所期望的。 这花了我一段时间,但得出的过程中,得到的形式元素,然后得到的领域是什么导致这些错过,因为没有形式。
<input>没有<form>是有效的,是的(至少对于HTML 4.01,看17.2.1的结尾):
用于创建控件的元素 通常出现在表单中 元素,但也可能出现在外部 的FORM元素声明 它们被用来建立用户 接口。这在 内在事件部分。请注意, 窗体之外的控件则不能 成功的控制。
我用W3C验证器检查了以下内容,并确认这是有效的。
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<input type='text' />
</body>
</html>
根据MDN,这是可能的:
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 form tag. Unfortunately, for the time being, the implementation of this feature across browsers is not yet good enough to rely on it.
参考最新的规范:
HTML 5.2 - W3C推荐标准(2017年12月14日)
与表单相关的元素可以与<form>元素有关系,该元素称为元素的表单所有者。如果与表单相关的元素没有与<form>元素相关联,则称其表单所有者为null。
I know this question is quite old, however, I have successfully built many complex data entry pages without form tags. Although it isn't considered "standard" by many, it is NOT inappropriate to use inputs without a <form>. My projects were instances where I needed complete control over how the page behaved and the default form behavior was getting in the way. Was able to perform page and field level validation ( using JS ) and "submitted" the data with Ajax calls etc...in fact, this is my preferred way these days.
需要大量的JS,但它不是那么困难,可以很容易地完成可重用的代码。
还有其他实例,我定义不使用表单输入,如登录页面。
希望这篇感言能帮助到一些人。
in my point of view , we can use input outside the form and even send data to the server without put the input in the form ,but for SEO and website readability(for visually impaired people)(just the same reason for the some semantic content tags in HTML5 , like section ,footer, header something like that ), we have to use input in the form tag.It is important that we ensure the code we use is available to all people including the visually impaired people because it is not just about websites, it is about providing access to information for everyone.