How to use onsubmit() to show a confirmation if there are multiple submit buttons on the same form?

前端 未结 5 614
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 07:29
5条回答
  •  我寻月下人不归
    2021-02-04 08:10

    Here's an event-listener based solution that avoids inline event handlers (useful if your site has a Content Security Policy that forbids inline JavaScript):

    HTML:

    
        
        
    
    

    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();
        }
    });
    

提交回复
热议问题