# JS 表單Event ## 相關函式 | 事件名 | 觸發時機 | |:----- |:------ | | submit | 點選送出表單時 | --- ## 範例 :::spoiler **submit event** HTML ```HTML! <form action="#" method="get" id="my_form"> <input type="text" id="my_text"> <button type="submit">資料送出</button> </form> ``` JS ```javascript! <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:  ###### tags: `frontend` `jsnote` [:arrow_right: Back to Front End Homepage :arrow_left:](/S-c0eqQmS16D8tcTx4ae1g)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up