如果你正在嘗試的onClick綁定到一個輸入類型=「提交」,那麼你就要有一個壞的時間。
不幸的是,即使您單擊該按鈕時返回false或e.preventDefault,表單仍會發送提交觸發器,因此一旦onClick代碼完成,它將提交。
例子:
<form action="woot.php" method="POST">
<input type="submit" value="submit" onClick="alert('You clicked me! How could you?! It's cool the form will still go to woot.php. return FALSE wont help you either.'); return FALSE;">
</form>
你可能想要做什麼:
<form action="woot.php" method="POST">
<input type="submit" value="Submit" onSubmit="alert('You aint goin nowhere!'); return FALSE;">
</form>
你應該做的:
<form action="woot.php" method="POST">
<input type="button" value="Button" onClick="alert('Away with you!'); window.location = 'http://www.google.com/';">
<input type="button" value="Button" onClick="someCoolFunction();">
</form>
如何重置形式?如何在按下輸入表單時使提交按鈕成爲點擊按鈕? – CaptainNemo
如果你要走這條路線,你可能會想要有幾個按鈕,每個功能都有一個。 –