简体   繁体   中英

Why does the code in my onclick event after a confirm not run?

Consider the following code:

<a href="#" onclick="return confirm(&quot;sure to remove?&quot;);;

new Ajax.Updater('12', '/admin/files/remove/id/12', {asynchronous:true, evalScripts:false});; 

return false;">Remove

</a>

It does not work, and I am not sure why.

How can I resolve this issue?

Nothing gets executed after a return statement. When you return, you return. That's what return is for. Thus no code after return confirm(&quot;sure to remove?&quot;); will ever run.

You probably want something like this:

<a href="#" onclick="
    if (confirm(&quot;sure to remove?&quot;)) {
        new Ajax.Updater('12', '/admin/files/remove/id/12', {asynchronous:true, evalScripts:false});
    }
    return false;">Remove</a>

Although it would be much better to create a proper function and call that instead.

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