2017-03-19 3 views
1

내 코드에 SweetAlert2를 구현하려고 시도했습니다. 트리거 버튼에서 함수를 호출하지 못했습니다.Sweetalert AJAX 실패

이 코드를 수정하려면 어떻게해야합니까?

$(".delete").click(function() { 

        swal({ 

         title: 'Are you sure?', 
         text: "You won't be able to revert this!", 
         type: 'warning', 
         showCancelButton: true, 
         confirmButtonColor: '#3085d6', 
         cancelButtonColor: '#d33', 
         confirmButtonText: 'Yes, delete it!' 
        }).then(function() { 
       delete_post($(this).attr("id")); 

         swal(
          'Deleted!', 
          'Your file has been deleted.', 
          'success' 
         ) 


       }) 

      }) 
     }); 

호출 된 함수 : 다음이 행하기 전에

function delete_post(id) { 
      var info = {"delete_post_id":id}; 
      $.post('index.php', 
       info 
      ).done(function(response) { 
       if(response["error"]) { 
        $('#error').html(response["error"]); 
       }else{ 
        window.location = "index.php"; 
       } 
      }); 
     } 
+0

delete_post가 호출되지 않았습니까? – CaptainHere

+0

예 그냥 삭제를 건너 뜁니다 @ILikeToMoveItMoveIt –

+0

응답 내용은 무엇입니까? 성공, swal() 전에 delete_post를 호출하십시오. 또한, 누락 된 세미 콜론에서 내 눈이 타 오릅니다! – cnorthfield

답변

1

@Halil이 말했듯이, this은 콜백 콜백에서 콜백의 버튼을 나타내지 않으며 콜백 함수 자체를 나타냅니다.

$(".delete").click(function() { 

    console.log('Delete button has been clicked...'); 

    // get the id 
    var id = $(this).attr("id")); 

    swal({ 
    title: 'Are you sure?', 
    text: "You won't be able to revert this!", 
    type: 'warning', 
    showCancelButton: true, 
    confirmButtonColor: '#3085d6', 
    cancelButtonColor: '#d33', 
    confirmButtonText: 'Yes, delete it!' 
    }).then(function() { 

    console.log('Confirm button clicked...'); 

    // use the id we declared earlier 
    delete_post(id); 

    swal(
     'Deleted!', 
     'Your file has been deleted.', 
     'success' 
    ); 

    }); 

}); 

function delete_post(id) { 

    console.log('Deleting post...', id); 

    var info = { 
    "delete_post_id": id 
    }; 

    $.post('index.php', info) 
    .done(function(response) { 

     console.log('Completed request...', response); 

     if (response["error"]) { 

     $('#error').html(response["error"]); 

     } else { 

     window.location = "index.php"; 

     } 

    }); 
} 

내가 일부 CONSOLE.LOG 디버깅을 추가 당신이 경우에 무슨 일이 일어나고 있는지 볼 수 있도록 : 당신과 같이 콜백에서 사용할 수 있도록 경고를 표시하기 전에

변수에 ID를 할당 브라우저에서 실행하십시오.

1

유형 console.info(this);는 :

delete_post($(this).attr("id")); 

내가 this 다음에 함수라고 생각합니다.

+0

이것은 댓글이어야합니다. 질문에 대한 답변이 아닙니다. –

+0

나는이 사이트에서 새로운 사람입니다. 나는 코멘트를 추가 할 수 없다. 그리고 마이너스 버튼을 클릭하면 댓글을 추가 할 수 없습니다. –

+0

예,하지만 그 이유는 다음과 같습니다 :-) 그리고 새 이민자가 답변으로 의견을 게시해야하는 것은 아닙니다. –