2017-03-19 6 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

あなたの応答は何を出力しますか?成功の前にdelete_postを呼び出すswal()。また、私の目は、欠けているセミコロンから燃える! – cnorthfield

答えて

1

@Halilは、thisがコールバックのアラートのため、コールバック内の自分のボタンを参照していないため、コールバック関数自体を参照しています。

あなたがそうのように、コールバックにそれを使用できるように、警告を表示する前に変数にIDを割り当てます。

$(".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のデバッグを追加しましたあなたのブラウザでそれを実行します。

1

タイプconsole.info(this);は:

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

私はthisが、その後にあなたの関数であると思います。

+0

これはコメントでなければなりません。質問への回答ではありません –

+0

私はこのサイトでは新しいです。私はコメントを追加することはできません。 マイナスボタンをクリックしてもコメントを追加することはできません。 –

+0

はい、それには理由があります:-)それは新人が解答としてコメントを投稿するべきではありません –

関連する問題