From 8d5596743635b94ac035b317b96edb72db3c06dc Mon Sep 17 00:00:00 2001 From: Jason Ren <40999116+jasonren0403@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:08:31 +0800 Subject: [PATCH 1/6] [zh-cn] init translation of html attribute required --- .../web/html/attributes/required/index.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 files/zh-cn/web/html/attributes/required/index.md diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md new file mode 100644 index 00000000000000..8d5d06687df177 --- /dev/null +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -0,0 +1,68 @@ +--- +title: HTML 属性:required +slug: Web/HTML/Attributes/required +l10n: + sourceCommit: aee2bd82de11cb7331134e48e8bd548bbedafcc5 +--- + +{{HTMLSidebar}} + +**`required`** [布尔](/zh-CN/docs/Glossary/Boolean/HTML)属性(如果存在)表示用户在提交自有表单前必须为输入指定一个值。 + +`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、 `{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、{{HTMLElement("input/file", "file")}} {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 + +{{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。也不支持任何按钮类型,包括 `image`。 + +注意,`color` 和 `range` 也不支持 `required`,因为它们都总是有一个值。`color` 类型默认为 `#000000`。`range` 的默认值是 `min` 和 `max` 之间的中点,如果没有声明,在大多数浏览器中,`min` 和 `max` 的默认值分别是 0 和 100。 + +对于由 {{HTMLElement("input/radio", "radio")}} 按钮组成的同名组,如果该组中的某个单选按钮具有 `required` 属性,则必须选中该组中的某个单选按钮,但该单选按钮不一定是应用了该属性的单选按钮。为改善代码维护,建议在组中的每个同名单选按钮中都包含 `required` 属性,否则一个都不包含。 + +如果是同名的一组 {{HTMLElement("input/checkbox", "checkbox")}} 输入类型,则只有带有 `required` 属性的复选框是必填的。 + +> [!NOTE] +> 设置 [`aria-required="true"`](/zh-CN/docs/Web/Accessibility/ARIA/Attributes/aria-required) 会告诉屏幕阅读器某个元素(任何元素)是必需的,但与元素的可选性无关。 + +### 属性交互 + +由于只读字段不能更改,因此 `required` 对同时指定了 [`readonly`](/zh-CN/docs/Web/HTML/Attributes/readonly) 属性的输入没有任何影响。 + +### 可用性 + +在包含 `required` 属性时,应在控件附近提供可见提示,告知用户 {{HTMLElement("input")}}、{{HTMLElement("select")}} 或 {{HTMLElement("textarea")}} 为必填项。此外,使用 {{cssxref(':required')}} 伪类来定位必填表单控件,并对其进行样式设计,以表明它们是必填的。这可以提高视力正常用户的可用性。辅助技术应根据 required 属性告知用户表单控件是必填的,但添加 `aria-required="true"` 也无妨,以避免浏览器/屏幕阅读器组合还不支持 `required`。 + +### 约束验证 + +如果元素为必填元素,且元素值为空字符串,则该元素会受到 {{domxref('ValidityState.valueMissing','valueMissing')}} 的影响,元素将匹配 {{cssxref(':invalid')}} 伪类。 + +## 无障碍考虑 + +向用户提供提示,告知他们需要使用表单控件。确保信息传递是多方面的,例如通过文字、颜色、标记和属性,这样,无论用户是色盲、存在认知差异还是使用了屏幕阅读器,都能理解相关要求。 + +## 示例 + +### HTML + +```html +
+
+ + +
+
+ + +
+ +
+``` + +### 运行结果 + +{{EmbedLiveSample('示例')}} + +## 参见 + +- {{domxref('validityState.valueMissing')}} +- {{cssxref(':required')}} 和 {{cssxref(':optional')}} +- {{htmlelement('input')}} +- {{htmlelement('select')}} From ca76bdac4b235034370c5cbf5914724e53acd38e Mon Sep 17 00:00:00 2001 From: Jason Ren <40999116+jasonren0403@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:09:35 +0800 Subject: [PATCH 2/6] Apply suggestions from code review --- files/zh-cn/web/html/attributes/required/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md index 8d5d06687df177..cabbba5bd7ee1a 100644 --- a/files/zh-cn/web/html/attributes/required/index.md +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -9,7 +9,7 @@ l10n: **`required`** [布尔](/zh-CN/docs/Glossary/Boolean/HTML)属性(如果存在)表示用户在提交自有表单前必须为输入指定一个值。 -`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、 `{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、{{HTMLElement("input/file", "file")}} {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 +`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、 `{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 {{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。也不支持任何按钮类型,包括 `image`。 From afeca36c5766baec00b952cfee58361680a995e2 Mon Sep 17 00:00:00 2001 From: Jason Ren <40999116+jasonren0403@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:12:44 +0800 Subject: [PATCH 3/6] Apply suggestions from code review --- files/zh-cn/web/html/attributes/required/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md index cabbba5bd7ee1a..a74c71ff828df1 100644 --- a/files/zh-cn/web/html/attributes/required/index.md +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -9,7 +9,7 @@ l10n: **`required`** [布尔](/zh-CN/docs/Glossary/Boolean/HTML)属性(如果存在)表示用户在提交自有表单前必须为输入指定一个值。 -`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、 `{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 +`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、`{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 {{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。也不支持任何按钮类型,包括 `image`。 From e8e1e4f89cc602cd472e8eb91424d73b8437c6b1 Mon Sep 17 00:00:00 2001 From: Jason Ren <40999116+jasonren0403@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:53:24 +0800 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: A1lo --- files/zh-cn/web/html/attributes/required/index.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md index a74c71ff828df1..a5138cdb7f2693 100644 --- a/files/zh-cn/web/html/attributes/required/index.md +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -7,9 +7,9 @@ l10n: {{HTMLSidebar}} -**`required`** [布尔](/zh-CN/docs/Glossary/Boolean/HTML)属性(如果存在)表示用户在提交自有表单前必须为输入指定一个值。 +**`required`** [布尔](/zh-CN/docs/Glossary/Boolean/HTML)属性(如果存在)表示用户在提交输入所属的表单前必须为其指定一个值。 -`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、`{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控制元素均支持 `required` 属性。如果这些输入类型和元素中出现了 {{cssxref(':required')}} 伪类,则将与之匹配。如果不包含该属性,则将匹配 {{cssxref(':optional')}} 伪类。 +`{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、`{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控件元素均支持 `required` 属性。如果这些输入类型和元素中出现了该属性,则会匹配 {{cssxref(':required')}} 伪类。如果不包含该属性,则会匹配 {{cssxref(':optional')}} 伪类。 {{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。也不支持任何按钮类型,包括 `image`。 @@ -17,7 +17,7 @@ l10n: 对于由 {{HTMLElement("input/radio", "radio")}} 按钮组成的同名组,如果该组中的某个单选按钮具有 `required` 属性,则必须选中该组中的某个单选按钮,但该单选按钮不一定是应用了该属性的单选按钮。为改善代码维护,建议在组中的每个同名单选按钮中都包含 `required` 属性,否则一个都不包含。 -如果是同名的一组 {{HTMLElement("input/checkbox", "checkbox")}} 输入类型,则只有带有 `required` 属性的复选框是必填的。 +如果是同名的一组 {{HTMLElement("input/checkbox", "checkbox")}} 输入类型,则只有带有 `required` 属性的复选框是必选的。 > [!NOTE] > 设置 [`aria-required="true"`](/zh-CN/docs/Web/Accessibility/ARIA/Attributes/aria-required) 会告诉屏幕阅读器某个元素(任何元素)是必需的,但与元素的可选性无关。 @@ -36,7 +36,7 @@ l10n: ## 无障碍考虑 -向用户提供提示,告知他们需要使用表单控件。确保信息传递是多方面的,例如通过文字、颜色、标记和属性,这样,无论用户是色盲、存在认知差异还是使用了屏幕阅读器,都能理解相关要求。 +向用户提供提示,告知他们特定表单控件是必填的。确保信息传递是多方面的,例如通过文字、颜色、标记和属性,这样,无论用户是色盲、存在认知差异还是使用了屏幕阅读器,都能理解相关要求。 ## 示例 @@ -56,7 +56,7 @@ l10n: ``` -### 运行结果 +### 结果 {{EmbedLiveSample('示例')}} From 2e377651689e01a3088d9f4885a53d9ddb4ef396 Mon Sep 17 00:00:00 2001 From: Jason Ren <40999116+jasonren0403@users.noreply.github.com> Date: Fri, 9 Aug 2024 09:02:05 +0800 Subject: [PATCH 5/6] Update index.md --- files/zh-cn/web/html/attributes/required/index.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md index a5138cdb7f2693..8ce320c405f4a5 100644 --- a/files/zh-cn/web/html/attributes/required/index.md +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -11,9 +11,7 @@ l10n: `{{HTMLElement("input/text", "text")}}`、`{{HTMLElement("input/search", "search")}}`、`{{HTMLElement("input/url", "url")}}`、`{{HTMLElement("input/tel", "tel")}}`、`{{HTMLElement("input/email", "email")}}`、`{{HTMLElement("input/password", "password")}}`、`{{HTMLElement("input/date", "date")}}`、`{{HTMLElement("input/month", "month")}}`、`{{HTMLElement("input/week", "week")}}`、`{{HTMLElement("input/time", "time")}}`、`{{HTMLElement("input/datetime-local", "datetime-local")}}`、`{{HTMLElement("input/number", "number")}}`、`{{HTMLElement("input/checkbox", "checkbox")}}`、`{{HTMLElement("input/radio", "radio")}}`、`{{HTMLElement("input/file", "file")}}` {{HTMLElement("input")}} 类型以及 {{HTMLElement("select")}} 和 {{HTMLElement("textarea")}} 表单控件元素均支持 `required` 属性。如果这些输入类型和元素中出现了该属性,则会匹配 {{cssxref(':required')}} 伪类。如果不包含该属性,则会匹配 {{cssxref(':optional')}} 伪类。 -{{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。也不支持任何按钮类型,包括 `image`。 - -注意,`color` 和 `range` 也不支持 `required`,因为它们都总是有一个值。`color` 类型默认为 `#000000`。`range` 的默认值是 `min` 和 `max` 之间的中点,如果没有声明,在大多数浏览器中,`min` 和 `max` 的默认值分别是 0 和 100。 +{{HTMLElement("input/range", "range")}} 和 {{HTMLElement("input/color", "color")}} 不支持该属性,也与之无关,因为两者都有默认值。`color` 的默认值为 `#000000`。`range` 的默认值为 `min` 和 `max` 之间的中点,如果没有声明,在大多数浏览器中,`min` 和 `max` 的默认值分别是 0 和 100。{{HTMLElement("input/hidden", "hidden")}} 也不支持该属性,因为用户不可能填写隐藏的表单。`required` 属性也不支持任何按钮 input 类型,包括 {{HTMLElement("input/image","image")}}。 对于由 {{HTMLElement("input/radio", "radio")}} 按钮组成的同名组,如果该组中的某个单选按钮具有 `required` 属性,则必须选中该组中的某个单选按钮,但该单选按钮不一定是应用了该属性的单选按钮。为改善代码维护,建议在组中的每个同名单选按钮中都包含 `required` 属性,否则一个都不包含。 From 9fca3d380f7ca03d1748d98a39bc1e9ca2ce12ce Mon Sep 17 00:00:00 2001 From: A1lo Date: Fri, 9 Aug 2024 09:17:54 +0800 Subject: [PATCH 6/6] update l10n.sourceCommit --- files/zh-cn/web/html/attributes/required/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/zh-cn/web/html/attributes/required/index.md b/files/zh-cn/web/html/attributes/required/index.md index 8ce320c405f4a5..930a9cc783f6e8 100644 --- a/files/zh-cn/web/html/attributes/required/index.md +++ b/files/zh-cn/web/html/attributes/required/index.md @@ -2,7 +2,7 @@ title: HTML 属性:required slug: Web/HTML/Attributes/required l10n: - sourceCommit: aee2bd82de11cb7331134e48e8bd548bbedafcc5 + sourceCommit: c66d3e5e446bf5ebfcef528c4a766bea6a08c879 --- {{HTMLSidebar}}