【问题标题】:JS, jQuery, how to confirm before deleting?JS,jQuery,删除前如何确认?
【发布时间】:2016-01-15 17:55:15
【问题描述】:

我有这段代码可以从 DB-ROW 中删除一条记录,并从 html 表中直观地删除它。

$(function() {
    $('.delpro').click(function(e) {
        var elem = $(this);
        var parents = $('.did' + elem.attr('data-id'));
        $.ajax({
            type: 'get',
            url: 'delete.php',
            data: 'id=' + elem.attr('data-id'),
            beforeSend: function() {
                elem.animate({ 'backgroundColor': '#fb6c6c' }, 400);
                parents.animate({ 'backgroundColor': '#fb6c6c' }, 400); 
            },
            success: function() {
                parents.slideUp(300,function() {
                    parents.remove();
                });
            }
        });
        return false;
    });
});

删除前我需要一个确认对话框。我是 JS 和 JQuery 的新手。我该如何做到这一点,以及将其放在代码中的什么位置?

【问题讨论】:

  • 试试js确认if(confirm("r u sure u want to delete!") == true){\\ur code here}

标签: javascript jquery confirm confirmation


【解决方案1】:

点击后立即放置:

$(function(){

  $('.delpro').click(function(e){

    if ( confirm('Are you sure') ) { //<--------------- here
      var elem = $(this);
      var parents = $('.did'+elem.attr('data-id'));


      $.ajax({
        type: 'get',
        url: 'delete.php',
        data: 'id='+elem.attr('data-id'),
        beforeSend: function() {
            elem.animate({'backgroundColor':'#fb6c6c'},400);
            parents.animate({'backgroundColor':'#fb6c6c'},400);


        },
        success: function() {



            parents.slideUp(300,function() {
            parents.remove();
            });


        }
     });
    return false;
   } //<--------------- here
});
});

【讨论】:

  • 谢谢!!有没有办法让对话框看起来更好看?
  • 也可以使用if(!confirm('Are you sure')) return false; 来减少标签,但这也很好。 +1
  • @IrkenInvader 是的,您的代码节省了很多,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多