简体   繁体   中英

Return confirm cancel button not working

I have this link in twig :

<a href="{{ path('relation-delete', {'id': c.getCustomerId}) }}" 
   onclick="return confirm('{% trans %}relation.delete{% endtrans %}');"
   class="tip" data-original-title="Verwijder klant {{ c.getCustomerName }}">

The HTML in the source :

<a href="/app_dev.php/projects/delete/1" class="tip" 
  data-original-title="Verwijder project Lantaarn plaatsen" 
  onclick="return confirm('Verwijderen');">

<button class="btn btn-danger"><i class="fa fa-times fa-fw"></i></button></a>`

the onlick confirm cancel button doesn't cancel the action but just keeps going. Somebody knows what's wrong with this return confirm ?

You can validade the confirm box outside of html element, in a function and, call this function on 'onclick' event. Like this:

<a href="somePage" onclick="return myFunction()">My link</a>

function myFunction() {
    if (confirm("Confirm message")) {
       // do stuff
    } else {
      return false;
    }
}

Adding event.preventDefault(); fixed it.

Here is an example:

console.log('Validating...');

function confirm_delete(){
var txt;
var r = confirm("Are you sure you want to delete?");
if (r == true) {
    txt = "You pressed OK!";
} else {
    txt = "You pressed Cancel!";
    event.preventDefault();
}
console.log(txt);
}

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