Search code examples
javascriptjqueryknockout.jsimpromptu

How to disable Jquery prompt save button on first click


I am using jQuery prompt to prompt a user to save. So, a user clicks a Save button, the Jquery prompt displays and asks if the user is sure he/she wants to save changes or not. The problem is, the user is able to click multiple times on the jQuery Save prompt button before the prompt closes. As a result, my save routine runs once for each time the prompt save button is clicked, which results in duplicate records in the database. I need to disable the prompt save button on the first click or simply close the prompt window on the first click to prevent multiple saves. How can I do this?

Client code:

$.prompt("Are you sure you want to save? You will not be able to change your decision after it's been saved.", {
    title: "Save?",
    buttons: { "Save": true, "Cancel": false },
    submit: function (e, v, m, f) {
        if (v) {
            response = saveUpdates(LoanDetails);
        }
    }
});

Solution

  • I created a fiddle here: http://jsfiddle.net/smurphy/4fqjR/

    The solution is a little convoluted. I thought there would be a simpler way to do it but after looking through the documentation I cannot find any built in feature.

    Here is the code:

    submit: function (value, message) {
            if (value) {
                $(message.prevObject)
                   .find('button[value="true"]')
                   .attr('disabled', true);
    
                //TODO: perform save 
                //response = saveUpdates(LoanDetails);
            }
        }
    

    The message param is a reference to the text description so you can find the button by navigating around the DOM from that starting point.