Html <input> 在没有 <form> 的情况下是否格式良好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3294572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:50:22  来源:igfitidea点击:

Is <input> well formed without a <form>?

htmlformsinput

提问by Greg Domjan

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

如果<input>没有在 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.

有一个标记页面中某些字段的过程,刚刚找到一个页面,其中包含未按我预期标记的输入字段。
我花了一段时间但发现获取表单元素然后获取字段的过程是导致这些被遗漏的原因,因为没有表单。

采纳答案by ChristopheD

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

<input>没有 a<form>出现有效,是的(至少对于 html 4.01,看看 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.

用于创建控件的元素通常出现在 FORM 元素内,但在用于构建用户界面时也可能出现在 FORM 元素声明之外。这将在有关内在事件的部分中讨论。请注意,窗体外的控件不能是成功的控件。

回答by Wai Yip Tung

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

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

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

回答by David

Reference to a more up-to-date specification:

参考更新的规范:

HTML 5.2 - W3C Recommendation (14 December 2017)

HTML 5.2 - W3C 建议(2017 年 12 月 14 日)

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.

一个与表单关联的元素可以与一个<form>元素有关系,这个元素被称为元素的表单所有者。如果与表单关联的元素不与<form>元素关联,则称其表单所有者为空。

回答by Adnan Faradhi

According to MDN it is possible:

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

请注意,始终可以在元素之外使用表单小部件,但如果这样做,则该表单小部件与任何表单都无关。这样的小部件可以在表单之外使用,但是你应该为这样的小部件制定一个特殊的计划,因为它们自己不会做任何事情。您必须使用 JavaScript 自定义它们的行为。

HTML5 在 HTML 表单元素上引入了 form 属性。它应该让您显式地将元素与表单绑定,即使它没有包含在表单标签中。不幸的是,就目前而言,跨浏览器实现此功能还不够好,无法依赖它。

回答by heisenberg

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

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

回答by G-Man

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.

我知道这个问题很老了,但是,我已经成功地构建了许多没有表单标签的复杂数据输入页面。尽管许多人不认为它是“标准的”,但使用没有<form>. 我的项目是我需要完全控制页面行为方式和默认表单行为方式的实例。能够执行页面和字段级别的验证(使用 JS)并使用 Ajax 调用等“提交”数据……事实上,这是我最近的首选方式。

Lots of JS is required, but its not that difficult and is easily done as reusable code.

需要大量的 JS,但它并不难,而且很容易作为可重用代码完成。

There are also other instances where I def do NOT use forms with inputs such as Login Pages.

还有其他一些情况,我不使用带有输入的表单,例如登录页面。

Hope this testimonial helps someone.

希望这个推荐对某人有帮助。

回答by taran

Sure, use

当然,使用

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

to check validity of field dynamically.

动态检查字段的有效性。