2015-10-14 54 views
0
<a class="btn btn-info" href="home/delete" onclick="return confirmDelete()"> 
    <i class="glyphicon glyphicon-edit icon-white"></i> 
    Delete 
</a> 

confirmDelete函数打开页面,不等待返回函数。我的功能是:javascript button onClick不等功能返回true并打开链接

function confirmDelete(){ 
    bootbox.confirm("Are you sure?", function(result) { 
    if (result) { 
     console.log("User confirmed dialog"); 
     return true; 
    } else { 
     console.log("User declined dialog"); 
     return false; 
    } 
}); 

但下面的代码工作正常,并等待ok打开链接。

<a class="btn btn-info" href="home/delete" onclick="return confirm()"> 
    <i class="glyphicon glyphicon-edit icon-white"></i> 
    Delete 
</a> 

回答

4

因为没有办法等待window.confirm这样的调用。您需要使用回拨进行确认,并设置窗口位置为您进行导航。

<a class="btn btn-info " href="home/delete" onclick="return confirmDelete(this);"> 
    <i class="glyphicon glyphicon-edit icon-white"></i>Delete</a> 

和使用GET请求删除警告更改到你的脚本

function confirmDelete (anchor) { 
    bootbox.confirm("Are you sure?", function(result) { 
     if (result) { 
      window.location.href = anchor.href; 
     } 
    }); 
    return false; 
} 

NOW是一个很坏很坏的坏主意。如果网页抓取工具或链接预加载器触击了该页面,它将向锚点发出请求并清除数据。

+0

谢谢,它工作:)而且我修改了我的原始代码的href它被加密之前发送与获取手段而不是href =“家/删除”其href =“家/删除/ asjfaskdjfksljfksafsaf”(一些ID ):) –