Html 删除按钮上的确认对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15435356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:29:25  来源:igfitidea点击:

Confirm dialog box on delete button

javascripthtmlangularjs

提问by FluxEngine

I am working with an app using Angular.

我正在使用 Angular 使用应用程序。

I currently have a delete button, that has a confirmation attached to onClick()

我目前有一个删除按钮,上面附有确认 onClick()

<a class="delete button" href="#" onClick="return confirm('Are you absolutely sure you want to delete?')" ng-click="remove_user(user, $event)" ></a>

The problem is when the confirmation pops up, both "OK' and "Cancel" delete the item, what am I missing to get "Cancel" to simply cancel?

问题是当确认弹出时,“确定”和“取消”都删除了该项目,我错过了什么才能让“取消”简单地取消?

回答by Robin Drexler

Maybe just using the angular click handler and doing the confirm logic there is worth a try.

也许只是使用角度点击处理程序并执行确认逻辑值得一试。

function FooController($scope, $window) {
  $scope.removeUser = function() {
    var deleteUser = $window.confirm('Are you absolutely sure you want to delete?');

    if (deleteUser) {
      $window.alert('Going to delete the user');
    }
  }
}

function FooController($scope, $window) {
  $scope.removeUser = function() {
    var deleteUser = $window.confirm('Are you absolutely sure you want to delete?');

    if (deleteUser) {
      $window.alert('Going to delete the user');
    }
  }
}
body {
  padding: 0;
  margin: 0;
  background: #3FA8C6;
  background-image: -moz-linear-gradient(top, #3fa8c6 0%, #3fa8c6 0%, #399ab2 100%);
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #3fa8c6), color-stop(0%, #3fa8c6), color-stop(100%, #399ab2));
  background-image: -webkit-linear-gradient(top, #3fa8c6 0%, #3fa8c6 0%, #399ab2 100%);
  background-image: -o-linear-gradient(top, #3fa8c6 0%, #3fa8c6 0%, #399ab2 100%);
  background-image: -ms-linear-gradient(top, #3fa8c6 0%, #3fa8c6 0%, #399ab2 100%);
  background-image: linear-gradient(to bottom, #3fa8c6 0%, #3fa8c6 0%, #399ab2 100%);
  color: #fff;
  font-family: 'Doppio One', sans-serif;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .3);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.wrapper {
  width: 50%;
  margin: 0 auto;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  letter-spacing: -0.03em;
  font-size: 2em;
}
a {
  border-bottom: 1px solid #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.7);
  padding-bottom: 0.15em;
  position: relative;
  color: white;
  text-decoration: none;
}
a:after {
  content: '';
  position: absolute;
  height: 1px;
  left: 0;
  right: 0;
  bottom: -2px;
  background: rgba(0, 0, 0, .1);
}
a:hover {
  color: #C0E3EC;
}
h1 {
  margin: 0.667em 0 0;
  padding-left: 0.5em;
  text-align: left;
}
h2 {
  font-size: 1.5em;
}
small {
  margin-top: 1em;
  display: block;
  font-style: italic;
  font-size: 0.667em;
}
p em {
  font-style: none;
}
#welcome {
  position: relative;
  overflow: hidden;
  padding-bottom: 1em;
  padding-left: 20px;
}
#welcome > div {
  padding-top: 1px;
}
#dave {
  float: left;
  margin-top: 3em
}
#welcome > h2 {
  margin-top: 0.5em;
  padding-left: 0.5em;
  margin-bottom: 0;
}
.bubble p {
  line-height: 22px;
}
.bubble {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.1);
  padding: 0.667em 1em;
  position: relative;
}
.bubble:after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  border-right: 20px solid white;
  border-right-color: inherit;
  top: 50px;
  left: -20px;
}
#features {
  margin: 0.444em 0 0;
  clear: both;
}
#features > h2 {
  margin: 0;
}
#features ol {
  position: relative;
  padding: 1em 0 1.5em;
  background: rgba(0, 0, 0, .1);
  border-color: rgba(0, 0, 0, .1);
  margin: 0;
  overflow: hidden;
  list-style: none;
  counter-reset: item;
}
#features li {
  width: 30%;
  padding: 0 1.5%;
  float: left;
  text-align: center;
  margin-bottom: 1em;
}
#features li h2 {
  display: block;
  padding: 1em;
  margin: 0.667em auto 1em;
  font-size: 1em;
  line-height: 1em;
  text-align: center;
  background: rgba(0, 0, 0, .1);
  border-radius: 2em;
  box-shadow: inset 0 0 1em rgba(0, 0, 0, .1), 0 2px 2px rgba(255, 255, 255, .1);
}
#next > div {
  width: 45%;
  float: left;
  padding: 0 2.5%;
}
img {
  z-index: 1;
  -webkit-transition: -webkit-transform 2s ease-in-out;
  -moz-transition: -moz-transform 2s ease-in-out;
  -o-transition: -o-transform 2s ease-in-out;
  -ms-transition: -ms-transform 2s ease-in-out;
  transition: transform 2s ease-in-out;
  position: relative;
}
img:active {
  -webkit-transform: rotate(1440deg) scale(1.2);
  -moz-transform: rotate(1440deg) scale(1.2);
  -o-transform: rotate(1440deg) scale(1.2);
  -ms-transform: rotate(1440deg) scale(1.2);
  transform: rotate(1440deg) scale(1.2);
}
@media screen and (max-width: 1200px) {
  .wrapper {
    width: 80%;
  }
}
@media screen and (max-width: 768px) {
  .wrapper {
    width: auto;
    margin: 0;
  }
  #welcome {
    margin-right: 2.5%;
  }
}
@media screen and (max-width: 500px) {
  .wrapper {
    text-align: center;
  }
  #dave {
    float: none;
  }
  #welcome {
    padding: 0 1.5em;
    margin: 0;
  }
  h1 {
    text-align: center;
    margin-bottom: 0;
    padding-left: 0;
    font-size: 1.8em;
  }
  #welcome > h2 {
    margin-bottom: 0.667em;
  }
  .bubble {
    text-align: center;
  }
  .bubble:after {
    display: none;
  }
  #features li {
    width: 47%;
  }
  #features li:last-child {
    float: none;
    clear: both;
    margin: 0 auto;
  }
  #next > div {
    float none;
    width: auto;
  }
}
<html ng-app ng-controller="FooController">

<body>
  <a class="delete button" href="#" ng-click="removeUser(user, $event)">Delete</a> 

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
</body>
</html>

回答by Shay

This is a little late but for future reader, like me 15 minutes ago:

这有点晚了,但对于未来的读者来说,就像 15 分钟前的我一样:

The "angular way" will be to create a directive as suggested here:

角度方式”将按照此处的建议创建指令:

'use strict';

angular.module('confirm', [])
    .directive('confirm', [function () {
        return {
            priority: 100,
            restrict: 'A',
            link: {
                pre: function (scope, element, attrs) {
                    var msg = attrs.confirm || "Are you sure?";

                    element.bind('click', function (event) {
                        if (!confirm(msg)) {
                            event.stopImmediatePropagation();
                            event.preventDefault;
                        }
                    });
                }
            }
        };
    }]);


Usage:

用法:

<button ng-click="save()" confirm="Save Changes?">Save</button>

回答by ningsuhen

If you are using bootstrap for UI, I'd recommend using angular-dialog-service. Once you include the module dependencies, you can confirm it like this.

如果您使用UI引导程序,我建议使用angular-dialog-service。包含模块依赖项后,您可以像这样确认。

function FooController($scope, $dialogs)
{
  $scope.removeUser = function() {
    var dlg = dialogs.confirm('Please Confirm', 'Are you absolutely sure you want to delete?');
    dlg.result.then(function () {
      console.log("User has confirmed");//do something when user confirms
    },function(){
      console.log("User has cancelled");//do something when user cancels. Can omit the 2nd function if no handling is required
    });
  }
}

See this examplefor more details.

有关更多详细信息,请参阅此示例

回答by Anil Singh

It might help to others

它可能对其他人有帮助

The HTML Code-sample

HTML 代码示例

<button confirmed-click="removeRowIndex(col, row)" 
        ng-confirm-box-click="Are you sure want to delete?" 
        class="btn btn-xs btn-default"/>

The AngularJs code-sample

AngularJs 代码示例

app.directive('ngConfirmBoxClick', [

      function () {
          return {
              link: function (scope, element, attr) {
                  var msg = attr.ngConfirmBoxClick;
                  var clickAction = attr.confirmedClick;
                  element.bind('click', function (event) {
                      if (window.confirm(msg)) {
                          scope.$eval(clickAction)
                      }
                  });
              }
          };
      }
]);

回答by Deo

Just replace the onclick methods like below and it will work.

只需替换如下所示的 onclick 方法即可。

onclick="javascript:return confirm('Are you absolutely sure you want to delete?')"

回答by Chris Ciollaro

<a class="delete button" href="#" onClick="return false" ng-click="if(confirm('Are you absolutely sure you want to delete?')){remove_user(user, $event);}" ></a>

Try this? I think it has to do with the fact that you're stopping the default "onclick" listener but not affecting the "ng-click" listener. Instead I moved the confirm logic into the "ng-click" listener and "return false" onclick because you don't want the link to take you away.

尝试这个?我认为这与您停止默认的“onclick”侦听器但不影响“ng-click”侦听器的事实有关。相反,我将确认逻辑移到“ng-click”侦听器中,并在单击时“返回 false”,因为您不希望链接将您带走。

回答by Anwar Saiah

Well I would create a function that will tick a global variable and check it on submit

好吧,我会创建一个函数来勾选一个全局变量并在提交时检查它

<form onsubmit='check_delete()'
<input type='submit' onclick='set_delete()' 

Javascript

Javascript

 function set_delete(){delete_flag = 1 ;} // this is a global var set by default to 0
 function check_delete(){if(delete_flag == 1) confirm(''sure?);}