简体   繁体   中英

OnClick Javascript Confirmation Window

Why does the below still delete even if I hit cancel on the alert popup? What am I missing?

onClick="confirm(\'Are you sure you want to delete '.esc_attr($this->event_name).'?\')"

If you return false from your onclick handler it will cancel the default action of the click. So try this:

onClick="return confirm(\'Are you sure you want to delete '.esc_attr($this->event_name).'?\')"

That will return whatever value the confirm() returns, ie, true if you click OK and false otherwise.

Check below..

<html>
<script language="javascript">
function checkMe() {
    if (confirm("Are you sure")) {
        alert("Clicked Ok");
        return true;
    } else {
        alert("Clicked Cancel");
        return false;
    }
}
</script>
<body>
<form name="myForm">
<input type=submit value="Press Me" onClick="return checkMe()">
</form>
</body>
</html>

Write what you want to do in Click Ok .

Good Luck!!!

You need to return the value from that confirm :

onClick="return confirm(\'Are you sure you want to delete '.esc_attr($this->event_name).'?\')"

So if you click cancel it will be equal to onClick="return false"

您需要从该确认返回值:

onClick="return confirm('Are you sure you want to delete?')"



You might need to make sure to use return false; when the user clicks on the cancel button, in your Javascript code. Otherwise, the script won't cancel the rest of the process.

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