如何在 HTML 中指定提交表單之前必須填寫該元素?


HTML 中的所需屬性用於指定在提交表單之前必須填寫該元素。如果未填寫該域並單擊提交按鈕,將引發錯誤,這會導致無法提交表單。錯誤將是“請填寫此域”。

required 屬性可以用於 input、select 和 textarea 元素。

示例

您可以嘗試執行以下程式碼來實現 readonly 屬性 −

<!DOCTYPE html>
<html>
   <head>
      <title>HTML placeholder attribute</title>
   </head>
   <body>
      <h2>Register</h2>
      <form action = "/new.php">
         <input type = "text" placeholder = "Student UserName" name = "name" required/><br>
         <input type = "submit" value = "Submit">
      </form>
   </body>
</html>

更新日期: 2020 年 3 月 3 日

680 次瀏覽

開啟你的職業生涯

完成本課程即可獲得認證

開始吧
廣告