返回确认取消按钮不工作


Return confirm cancel button not working

我有这个链接:

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

源文件中的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>`

onclick确认取消按钮不会取消操作,而是继续进行。有人知道这个return confirm有什么问题吗?

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

<a href="somePage" onclick="return myFunction()">My link</a>
function myFunction() {
    if (confirm("Confirm message")) {
       // do stuff
    } else {
      return false;
    }
}

添加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);
}