Kieran Hanna
Kieran Hanna

Reputation: 175

javascript confirm (cancel) still submits form when returning false

When I submit and click OK it continues but when I press "cancel" it submits anyway. I tried using this code but the submit and cancel button still do the same thing.

model.saveForm = function() {
    var r = confirm("Press a button");
    alert(p);
    if (r == true) {
        x = "You pressed OK!";

        if (!validateRequiredNew()) return;
        model.saving(true);

        // check if group id is needed
        var generatedGroupIdPromise = null;
        // if (model.selectedUsers().length > 1 || model.selectedUsers()[0].selectedRoles().length > 1) {
        generatedGroupIdPromise = $.ajax({
            url: '../_vti_bin/listdata.svc/GroupIdGeneratorList',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{Title: 'dummy'}",
            processData: false,
            dataType: "json"
        });                         

    } else if (r == false){
        x = "You pressed Cancel!"; 
    }

And the aspx page:

<button data-bind="click: saveForm, visible: !saving()">Submit Request</button>

Upvotes: 0

Views: 1596

Answers (1)

Sherali Turdiyev
Sherali Turdiyev

Reputation: 1743

Just put return false; or event.preventDefault(); into your method.

Upvotes: 3

Related Questions