简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM