要在HTML中的元素无效时执行脚本,请使用oninvalid 属性。
您可以尝试运行以下代码来实现oninvalid 属性。
<!DOCTYPE html> <html> <head> <title>HTML oninvalid attribute</title> </head> <body> <h2>ID</h2> <form action = "/new.php"> <input type = "text" placeholder = "Student ID" name = "sname" oninvalid = "alert('Fill it first!')" required /><br> <input type = "submit" value = "Submit"> </form> </body> </html>