2011年2月21日 星期一

HTML: <button> 要記得給 type !!!

同事前幾天遇到了個問題,他在網頁 form 裡面加了個 button,然後在 button 加上了 onClick event。
但執行網頁時,browser 卻自動跑到 form 裡面設定的 action @@

到 W3School 查了一下,發現

"Always specify the type attribute for the button. Different browsers uses different default values for the type attribute."

哦!?該不會 browser 預設把 form 給送出去了吧?
來做個實驗…


<html>
<body>

<form action="http://www.google.com.tw">
<button type="button">Let's Go!</button>
</form>

</body>
</html>

給了 type 之後,就只是一個 button,不會亂做事XD


<html>
<body>

<form action="http://www.google.com.tw">
<button>Let's Go!</button>
</form>

</body>
</html>

如果沒有加 type...那一按下去馬上就跑去 google 囉!

沒有留言:

張貼留言