2013-08-03 32 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' /> 

khi người dùng nhấp vào nút gửi thứ hai, ví dụ: No tôi muốn hiển thị hộp thoại xác nhận là "Bạn có chắc chắn muốn thực hiện giao dịch không."Cách sử dụng onsubmit() để hiển thị xác nhận nếu có nhiều nút gửi cùng một biểu mẫu?

+0

có thể trùng lặp của [Trình kích hoạt Javascript với biểu mẫu có nhiều nút gửi] (http://stackoverflow.com/questions/13707633/javascript-onsubmit-with-form-with-multiple-submits-buttons) – totymedli

Trả lời

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> 

Đã làm việc tốt. vừa thay đổi onsubmit() thành onclick(). như chức năng của cả hai trong tình huống này là như nhau.

0

Chỉ cần sử dụng hai trong cùng một biểu mẫu. Một cho mỗi nút:

<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> 

Ngoài ra, nếu bạn muốn thực hiện nghiên cứu của bạn, bạn sẽ tìm thấy những:

+0

điều này là phổ biến. là có anyway để làm điều đó với nhiều nút gửi trong một hình thức duy nhất? – RatDon

2

Bạn có thể liên kết với onclick thay vì tiện ích - xem bên dưới.

<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> 

Hoặc luân phiên, sử dụng jQuery:

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

Tôi nghĩ rằng câu hỏi muốn các nút cần có các thông báo xác nhận khác nhau. –

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

Tôi wouldnt cố gắng tạo ra một trình mà đúng hơn chỉ là một nút mà có một onclick = "function()" và sau đó sử dụng javascript để thiết lập một biến để xem có bao nhiêu lần họ đã nhấp vào nó và một cảnh báo();

hy vọng điều này giúp: D

1

Dưới đây là một giải pháp dựa trên sự kiện người nghe mà tránh xử lý sự kiện inline (hữu ích nếu trang web của bạn có một chính sách an ninh Nội dung cấm inline 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(); 
    } 
}); 
Các vấn đề liên quan