2016-08-11 4 views
1

クリックした要素の親を削除します。私は、最初の警告を取得するために甘いアラートを使用して、私は、AJAX機能を呼び出すと、私は成功機能で要素を取得したい後:

これは私の関数である:

function removeImage(id) { 
     var element = $(this).closest('.dropzone'); 

    swal({ 
     title:"Delete", 
     text: "delete", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes !", 
     cancelButtonText: "No, cancel", 
     closeOnConfirm: false, closeOnCancel: false 
    }, function(isConfirm){ 
     if (isConfirm) { 
     $.ajax({ 
      type: "POST", 
      data: {id: id}, 
      url:'ajax/deleteimage.php', 
      success : function(data){ 
      if(data == 'ok'){ 
        swal({ 
       title:"Delete", 
       text: "delete", 
       type: "success",  
       confirmButtonColor: "#AEDEF4", 
       confirmButtonText: "Ok", 
       closeOnConfirm: true, 
       }, function(isConfirm){ 
        $.when($('.dropzone').find("#"+id).parent().fadeOut()) 
             .done(function() { 
          $('.dropzone').find("#"+id).parent().remove(); 
         }); 
         var n_div = $('.dz-image-preview').length-1; 
          if (n_div === 0) { 
          $('.dz-message').css("opacity",'1'); 
          } 
       }); 
      }else{ 
       swal("Error, try again", "", "error"); 
      } 
       } 
     }); // end ajax call 

     } else {  
     swal("Cancel", "", "error"); 
     } 
    }); 


    } 

私は変更することはできません。変数要素を含む成功関数内の$('.dropzone')

+1

の可能性のある重複した[コールバック内の正しい\ 'この\' /コンテキストにアクセスする方法?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct- this-context-inside-a-callback) –

+0

クリックされた要素が何であるかは不明です。関数は 'id'を受け取りますが、関数がクリックハンドラであるかのように、' this'を使用しようとします。 –

+0

関数がどのように呼び出されるかを示すのに役立ちます。 –

答えて

1

jQueryで同じ問題が発生しました。主な問題は、ajaxがswal内のこのコンテキストに直接アクセスできないことです。したがって、このコンテキストをajaxに渡す必要があります。最初に、このコンテキストを変数に格納し、そのコンテキストをajaxの中に割り当てます。

$(".delete_category").click(function() { 
    var call_url = $(this).val(); 
    var this_context = $(this); 
    swal({ 
     title: "Are you sure?", 
     text: "Your may not be able to recover!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonClass: "btn-danger", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }, 
     function() { 
      $.ajax({ 
       context: this_context, 
       method: "POST", 
       url: call_url, 
       success: function (response) { 
        if (response == "success") { 
         swal("Deleted!", "Record deleted successfully.", "success"); 
         $(this).closest('.delete_row').hide(); 
        } else { 
         swal("Error!", response, "error"); 
        } 
       } 
      }); 

     } 
    ); 
}); 
関連する問題