JS,jQuery,删除之前如何确认?

时间:2016-01-15 17:55:15

标签: javascript jquery confirm confirmation

我有这个代码从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的新手。我该怎么做,把它放在代码里面?

1 个答案:

答案 0 :(得分:2)

点击后立即放置它:

$(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
});
});