2013-08-03 42 views
8
<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"> 
<input type='submit' name='delete' value='Undo' /> 
<input type='submit' name='no' value='No' /> 

當用戶點擊第二個提交按鈕時,即No我想顯示確認對話框爲「您確定要提交交易嗎?」。如何使用onsubmit()來顯示確認是否在同一個表單上有多個提交按鈕?

+0

[Javascript onsubmit with multiple submits buttons]可能重複(http://stackoverflow.com/questions/13707633/javascript-onsubmit-with-form-with-multiple-submits-buttons) – totymedli

回答

19
<form method='post'> 
    <input type='submit' name='undo' value='Undo' onclick="return confirm('Are you sure you want to rollback deletion of candidate table?')"/> 
    <input type='submit' name='no' value='No' onclick="return confirm('Are you sure you want to commit delete and go back?')"/> 
</form> 

工作正常。 只是將onsubmit()更改爲onclick()。因爲在這種情況下兩者的功能是相同的。

0

只需使用兩個相同的形式。爲每個按鈕:

<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"> 
    <input type='submit' name='delete' value='Undo' /> 
</from> 
<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"> 
    <input type='submit' name='no' value='No' /> 
</from> 

另外,如果你會做你的研究,你會發現這些:

+0

這很常見。無論如何,有多個提交按鈕在一個單一的形式嗎? – RatDon

2

您可以綁定到onclick而不是onsubmit - 請參見下文。

<script> 
function submitForm() { 
    return confirm('Rollback deletion of candidate table?'); 
} 
<script> 

<form> 
    <input type='submit' onclick='submitForm()' name='delete' value='Undo' /> 
    <input type='submit' onclick='submitForm()' name='no' value='No' /> 
</form> 

或者交替,使用jQuery:

<script> 
$(document).ready(function() { 
    $('form input[type=submit]').click(function() { 
     return confirm('Rollback deletion of candidate table?'); 
    }); 
}); 
<script> 
+1

我想這個問題想要的按鈕應該有不同的確認消息。 –

0
<form onsubmit="submitFunction();"> 
    <input type='submit' name='delete' value='Undo' /> 
    <input type='button' onclick="declineFunction()" name='no' value='No' /> 
</form> 

我不會嘗試創建一個提交而只是具有的onclick =按鈕 「功能()」,然後使用JavaScript來設置一個變量來查看他們點擊了多少次,然後一個alert();

希望這有助於:d

1

下面是一個基於事件偵聽器的解決方案,避免了內聯事件處理程序(有用的,如果你的網站有一個禁止內聯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(); 
    } 
}); 
相關問題