简体   繁体   中英

Actionlink with confirmation dialog

I want to make a confirmation message appear before the link opens. However, despite this, the link is followed despite clicking on cancel or closing the dialog. Please help me, as I am stuck with this.

<div style="float: left; width: 40px; height: 10px; "> @Html.ActionLink("-Pg", "SupprimerPage", "Section", new { pageId = @item.Id }, new { onclick = "ConfirmerSuppressionPage(event);", @class = "editLink", style = "width:30px" })</div>

Javascript:

 function ConfirmerSuppressionPage(event) {
    var x = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
    console.log(x);
    if (x == null) {
        event.preventDefault();
        return false;
    }

    if (x == true) {

        return true;
    }
    else {
        event.preventDefault();
        event.stopPropagation();
    }
}

This may not help, but I have had a similar problem of certain browsers not respecting the cancel button, and created a replacement function to solve it:

function showConfirm(str) {
    if (confirm(str) == false) {
        if (typeof (event) != "undefined") {
            event.returnValue = false;
        }

        return false;
    }
    else {
        return true;
    }
}

Then in your ConfirmerSuppressionPage function, replace the call to confirm with showConfirm

var result = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
if (result == true) {
    x = "You pressed OK!";
} else {
    x = "You pressed Cancel!";
}

Confirm is a JavaScript dialog box and it will return true or false . And JavaScript will not execute next line until user will not click on Yes or No button of dialog.

Any how Confirm will return you True or False, and now you have to deal with it.

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