SweetAlert与ajax结合使用

时间:2017-07-29 07:51:47

标签: javascript jquery ajax

我有ajax删除功能,但它保持不能与我的sweetalert,我不知道我的代码有什么问题,看不到任何错误的地方。请告诉我如何修改它。

function deletei(){

    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!'
    },function ($rfno,$user) {
		theuser = $user;
		   therfno = $rfno;
		   
	         $.ajax ({
           type: "POST",
           url: "updateleave.php",
           data: {RefNo: $rfno, userid: $user},
		   success: function () {
		   swal('Deleted!', 'Your file has been deleted!', 'success')
    }
	    
          });
    });
  } 
<input type="button" value="button" onClick="deletei(\'' .$poarr[$i]['RefNo']. '\',\''.$poarr[$i]['StaffId'].'\')" >

4 个答案:

答案 0 :(得分:1)

showCancelButton:true,已弃用。我建议使用按钮。然后,您可以创建带有所需按钮的数组。

答案 1 :(得分:0)

   function deletei(user,rfno){
var theuser = user;
var therfno = rfno;
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!'
},function () {
    $.ajax ({
        type: "POST",
        url: "updateleave.php",
        data: {RefNo: therfno, userid: theuser},
        success: function () {
            swal('Deleted!', 'Your file has been deleted!', 'success')
        }

    });
});
}
<input type="button" value="button" onClick="deletei(\'' .$poarr[$i]['RefNo']. '\',\''.$poarr[$i]['StaffId'].'\')" >

试试这个。
修改: - 从$rfno

中删除 $

修改: - 您没有在username函数中传递regNodeletei的值。

答案 2 :(得分:0)

您使用deletei$poarr[$i]['RefNo']两个参数调用$poarr[$i]['StaffId'],但不在deletei中使用它们。我怀疑这些论点应该是theusertherfno的价值?

function deletei($rfno, $user){
  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!'
  }, function () {
    $.ajax ({
      type: "POST",
      url: "updateleave.php",
      data: {RefNo: $rfno, userid: $user},
      success: function () {
        swal('Deleted!', 'Your file has been deleted!', 'success')
      }
    });
  });
}

答案 3 :(得分:0)

所以我已经为我目前的状况更新了我的成功答案。希望你们可以参考一下,我没有把这个库添加到小提琴中,所以你们可能只是复制这些代码并自行修改。谢谢所有提供建议的人对我来说!

&#13;
&#13;
function deletei($refnos,$users){
			
			var refId = $refnos;
			var userId = $users;
			SwalDelete(refId,userId);
			e.preventDefault();
		
	}
	
		

function SwalDelete(refId,userId){
		 
  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!',
 preConfirm: function() {
	 return new Promise(function(resolve) {  
    $.ajax ({
      type: "POST",
      url: "updateleave.php",
      data: {RefNo: refId, userid: userId},
	  success: function(data){
		    swal('Deleted!', 'Your file has been deleted!', 'success'); 
			var tbl = document.getElementById("myTable");
         for (var i=0; i < tbl.rows.length; i++) {
            var trs = tbl.getElementsByTagName("tr")[i];
             var cellVal=trs.cells[0].innerHTML;
              if (cellVal=== refId)  {
               document.getElementById("myTable").deleteRow(i);
			  			   
			   break; }
           }
                    },
					});
					  
  });
},
});
 
} 
&#13;
<button type="button" onClick="deletei(\'' .$poarr[$i]['RefNo']. '\',\''.$poarr[$i]['StaffId'].'\')" ></button>
&#13;
&#13;
&#13;