下面是一個基於事件偵聽器的解決方案,避免了內聯事件處理程序(有用的,如果你的網站有一個禁止內聯JavaScript中的內容安全策略):
HTML:
<form method="post">
<input type="submit" id="deleteButton" name="delete" value="Undo" />
<input type="submit" id="noButton" name="no" value="No" />
</form>
JS:
document.getElementById("deleteButton").addEventListener("click", function(evt) {
if (!confirm("Are you sure you want to rollback deletion of candidate table?")) {
evt.preventDefault();
}
});
document.getElementById("noButton").addEventListener("click", function(evt) {
if (!confirm("Are you sure you want to commit the transaction?")) {
evt.preventDefault();
}
});
[Javascript onsubmit with multiple submits buttons]可能重複(http://stackoverflow.com/questions/13707633/javascript-onsubmit-with-form-with-multiple-submits-buttons) – totymedli