9

私のAjaxメソッドは次のようになります

$.post(url,{
            ajax_call:"putDonation",
            addresse:addresse,
            phone:phone,
            email:email,
            name:name,
            amount:amount,
            amountinwords:amountinwords
           }).done(function(data){

            console.log(data);

            var arr = [];

            try {
                  var str = "";
                  //convert to json string
                      arr = $.parseJSON(data); //convert to javascript array
                      $.each(arr,function(key,value){
                        str +="<li>"+value+"</li>";
                    });
                       alert(str);
                       $(".alert-danger").show();
                       $("#error").html(str);

              } catch (e) {
                  swal("Jay Gayatri !", 'Donation Sucessful', "success")
                  $("#donation")[0].reset();
              }


           })

このような甘いアラート警告ポップアップを表示したい

   swal({   
                title: "Are you sure ?",   
                text: "You will not be able to recover this imaginary file!",   
                type: "warning",   
                showCancelButton: true,   
                confirmButtonColor: "#DD6B55",   
                confirmButtonText: "Yes, delete it!",   
                closeOnConfirm: false 
              }, 
                function(){   
                  swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
                });

キャンセルをクリックすると、ajax 呼び出しは行われません。[はい] を選択すると、呼び出しのみが行われます。

Sweet Alertメソッド内にAjaxメソッドを埋め込む方法を教えてください

ありがとう

4

4 に答える 4

23

簡単な例として、私が自分のサイトでどのようにそれを行ったかを示すことができます. Ajax 呼び出しをスイート アラート内に配置しました。

    function deleteorder(orderid) {
        swal({
          title: "Are you sure?", 
          text: "Are you sure that you want to cancel this order?", 
          type: "warning",
          showCancelButton: true,
          closeOnConfirm: false,
          confirmButtonText: "Yes, cancel it!",
          confirmButtonColor: "#ec6c62"
        }, function() {
            $.ajax(
                    {
                        type: "post",
                        url: "/admin/delete_order.php",
                        data: "orderid="+orderid,
                        success: function(data){
                        }
                    }
            )
          .done(function(data) {
            swal("Canceled!", "Your order was successfully canceled!", "success");
            $('#orders-history').load(document.URL +  ' #orders-history');
          })
          .error(function(data) {
            swal("Oops", "We couldn't connect to the server!", "error");
          });
        });
       }

したがって、確認ボタンを押した場合にのみ ajax 呼び出しが行われます。これが、必要な方法でコードを配置するのに役立つことを願っています。

于 2015-10-28T10:35:52.860 に答える
2

サイトリファレンスにあります-

swal({   title: "Ajax request example",   
    text: "Submit to run ajax request",   
    type: "info",   showCancelButton: true,   
    closeOnConfirm: false,   
    showLoaderOnConfirm: true, 
}, 
function(){   
   $.post(url,data,callback)
});
于 2015-10-28T10:43:07.050 に答える