JS 表單Event

相關函式

事件名 觸發時機
submit 點選送出表單時

範例

submit event

HTML

<form action="#" method="get" id="my_form">
    <input type="text" id="my_text">
    <button type="submit">資料送出</button>
</form>

JS

<script>
    let my_form = document.getElementById("my_form");
    my_form.addEventListener("submit", function(e){
        let my_text = document.getElementById("my_text");
        if(my_text.value == ""){
            e.preventDefault();
            alert("需要輸入資料");
        }
    });
</script>
  • 說明:應用 preventDefault() 函式停止表單送出的預設行為
  • Demo:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
tags: frontend jsnote Back to Front End Homepage
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →