简体   繁体   English

JavaScript确认框工作错误

[英]Javascript Confirm box working wrong

Hi all I am working in angular js , I want to add confirm message when click delete button 大家好,我在angular js工作,我想在单击Delete按钮时添加确认消息

I tried this code : 我尝试了这段代码:

<a data-ng-click="removeTodo(this)" href="javascript:;"
onclick="javascript:return confirm('Are you sure you want to delete ?')">Delete</a>

This is shown ok and cancel button with the confirm box , if i click the ok button it's going to next process , but if i click the cancel button then it's same as going to same process confirm box was not hide ( The ok button will return true , but same as cancel button will be return true not a false) 这将显示确定并带有确认框的“取消” 按钮 ,如果我单击“ 确定”按钮,它将进入下一个过程,但是如果我单击“ 取消”按钮,则与进入同一过程的确认框相同,确认框未隐藏( 确定按钮将返回true,但与取消按钮相同,将返回true而不是false)

But it's working good when i used this below code on click my delete button 但是当我在单击删除按钮时使用以下代码时,它的工作效果很好

 $scope.removeTodo = function (Student) {
        if (!confirm("Do you want to delete this ? ")) {
            return false;
        }

   //please think , I added Some code for next process in here 
};

What is the problem ? 问题是什么 ? what i missed ? 我错过了什么? Why my first code was not working ? 为什么我的第一个代码不起作用?

It is because return false is same as preventing the default action and stopping event propagation - which will prevent the handlers attached to ancestor elements from being executed... but in your case both click handlers are attached to the same element. 这是因为return false等同于阻止默认操作并阻止事件传播-这将阻止执行附加到祖先元素的处理程序...但是在您的情况下,两个click处理程序都附加到了同一元素。

So even if the confirm returns false the angular click handler will get executed. 因此,即使确认返回false,也将执行有角单击处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM