JavaScript 中 oninvalid 事件的用法是什麼?


如果 <input> 中新增的欄位值無效,則觸發 oninvalid 事件。如果使用者忘記在提交前填寫表單,新增一條訊息。

示例

你可以嘗試執行以下程式碼,瞭解如何在 JavaScript 中實現 oninvalid 事件。

<!DOCTYPE html>
<html>
   <body>
      <script>
         function resetFunct() {
            alert("The form was reset");
         }
      </script>
     
       <form>
         Enter Name: <input type="text"
         required oninvalid = "alert('Fill the form before submitting!');" >  <br>
         Enter birth month: <input type=  "text"> <br>
         <input type = "submit">
         <input type = "reset">
      </form>
   </body>
</html>

更新於:2020 年 5 月 22 日

237 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.