在 HTML 中為表單控制元件新增標籤?


在 HTML 中,<label> 標籤用於定義像 <input>、<progress>、<select>、<meter>、<textarea> 等一系列元素的標籤。

<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="search">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
<meter>
<progress>
<select>
<textarea>

語法

以下是 HTML 中 <label> 標籤的用法 −

<label> form content... </label>

<label> 標籤使用 for 和 form 屬性,現在讓我們來看看 for 和 from 屬性的值和描述。

  • for − 指定標籤相關聯的表單元素的 id。以下是 label 中 for 屬性的語法 -

<label for=”element_id”>
  • form − 使用此屬性時,有多個表單,它指定標籤屬於哪個表單。讓值是 form_id,以下是 label 中 form 屬性的語法 -

<label form="form_id">

示例

在以下示例中,我們將建立一個表單,併為其中的控制元件新增標籤。

<!DOCTYPE html>
<html>
<body>
   <h1>Welcome to TutorialsPoint</h1>
   <h2>The label for attribute</h2>
   <p>Click on one of the radio button:</p>
   <form action="/action_page.php">
      <input type="radio" id="html" name="language" value="HTML">
      <label for="html">HTML</label>
      <br>
      <input type="radio" id="css" name="language" value="CSS">
      <label for="css">CSS</label>
      <br>
      <input type="radio" id="javascript" name="language" value="JavaScript">
      <label for="javascript">JavaScript</label>
      <br>
      <br>
      <input type="submit" value="Submit">
   </form>
</body>
</html>

示例

讓我們看另一個示例 −

<!DOCTYPE html>
<html>
<body>
   <h1>Welcome to TutorialsPoint</h1>
   <h2>The label for form attribute</h2>
   <p>The label in the second radio button is outside the form, but still a part of the form. Try to click the "Teacher" text label to select the "Teacher" radio button.</p>
   <form action="/action_page.php" id="form1">
      <input type="radio" id="student" name="person" value="STUDENT">
      <label for="student">STUDENT</label>
      <br>
      <input type="radio" id="teacher" name="person" value="TEACHER">
      <br>
      <input type="radio" id="admin" name="person" value="ADMIN">
      <label for="admin">ADMIN</label>
      <br>
      <br>
      <input type="submit" value="Submit">
   </form>
   <br>
   <label form="form1" for="teacher">TEACHER</label>
</body>
</html>

示例

在此示例中,我們嘗試建立一個示例登入頁面併為其新增標籤。

<!DOCTYPE html>
<html>
<head>
   <title>HTML label Tag</title>
</head>
<body>
   <label for="email">EMAIL-ID: <br />
   <input type="email" value="" name="emailid" size="30" placeholder="Enter a valid email address">
   <br />
   <br />
   <label for="phone">PHONE NO: <br />
   <input type="text" value="" name="phno" size="30" maxlength="10" placeholder="Enter a valid phone number" pattern="[0-9]{10}">
   <br />
   <br />
</body>
</html>

更新於: 10-Oct-2023

387 次瀏覽

開啟你的 職業生涯

完成該課程以獲得認證

開始學習
廣告
© . All rights reserved.