Lex
Lex

Reputation: 891

mvc javascript confirm before postback not working

On one of my pages, I need to make sure that the user has printed the page before continuing, so I intercept the button click with a simple onclick method, this prompts the user to confirm if they have printed the page or not.

If they have printed the page, I want to continue and do the postback, however if the user presses cancel, I expect the user to stay on the current page. This is not happening, currently even when pressing cancel, the page is still going back to the server, and the user is shown the next screen.

Button:

<button type="submit" name="Continue" value="Continue" class="button" onclick="clientClick();">Continue</button>

Javascript function to alert the user:

function clientClick() {
        if (confirm("MESSAGE")) {
            return true;
        }
        return false;
    }

Upvotes: 1

Views: 828

Answers (2)

Igor
Igor

Reputation: 15893

onclick="return clientClick();"

Consider the code you put as value of onclick attribute as the content of an anonymous function:

onclick="(function(){ return clientClick(); })();"

Upvotes: 5

shayah
shayah

Reputation: 39

instead of type="submit" set type="button"

and in on click function

function clientClick() {
    if (confirm("MESSAGE")) {    $("#your_form_id").submit();
    }
    return false;
}

Upvotes: 0

Related Questions