有<input>而不是<form>是有效的吗?

有一个在页面中标记一些字段的过程,只是发现了一个输入字段的页面,那里没有被标记为我所期望的。 这花了我一段时间,但得出的过程中,得到的形式元素,然后得到的领域是什么导致这些错过,因为没有形式。


当前回答

是的,您可以在没有表单的情况下获得有效的输入。

其他回答

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,但它不是那么困难,可以很容易地完成可重用的代码。

还有其他实例,我定义不使用表单输入,如登录页面。

希望这篇感言能帮助到一些人。

我用W3C验证器检查了以下内容,并确认这是有效的。

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
</head>
<body>
  <input type='text' />
</body>
</html>

<input>没有<form>是有效的,是的(至少对于HTML 4.01,看17.2.1的结尾):

用于创建控件的元素 通常出现在表单中 元素,但也可能出现在外部 的FORM元素声明 它们被用来建立用户 接口。这在 内在事件部分。请注意, 窗体之外的控件则不能 成功的控制。

根据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.

是的,您可以在没有表单的情况下获得有效的输入。