返回确认取消按钮不起作用

Tom*_*mie 0 html javascript php symfony 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 }}">
Run Code Online (Sandbox Code Playgroud)

源代码中的HTML:

<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>`
Run Code Online (Sandbox Code Playgroud)

onlick确认取消按钮不会取消操作,而是继续执行。有人知道此退货确认有什么问题吗?

Dhr*_*ruv 6

添加event.preventDefault();修复了它。

这是一个例子:

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);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以在函数中验证html元素外部的确认框,并在“ onclick”事件中调用此函数。像这样:

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

function myFunction() {
    if (confirm("Confirm message")) {
       // do stuff
    } else {
      return false;
    }
}
Run Code Online (Sandbox Code Playgroud)