簡體   English   中英

甜蜜警報確認 - 獲取用戶響應

[英]Sweet Alert Confirm - Get user response

我有一個JS文件,我有這樣的函數:

function eliminarProducto(id){
  var url = '../php/apartado/elimina_producto.php';
  var pregunta = confirm('Are you sure to delete this user?');

瀏覽器中顯示的消息看起來很簡單,所以我想通過Sweet Alert消息來改變它......但我不知道怎么做!

if(pregunta==true){
  $.ajax({
    type:'POST',
    url:url,
    data:'id='+id,
    success: function(registro){
      $('#agrega-registros').html(registro);
      return false;
    }
  });
  return false;
}else{
  return false;
}

***********************編輯后************************* *****新代碼

function eliminarProducto(id){
  var url = '../php/apartado/elimina_producto.php';

  swal({
    title: '¡Atención!',
    text: "¿Desea eliminar el registro?",
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Aceptar'
  }).then(function(){
    $.ajax({
      type:'POST',
      url:url,
      data:'id='+id,
      success: function(registro){
        $('#agrega-registros').html(registro);
      }
    });
  })
}

如果我選擇取消選項,我怎么能打破行動??? :)

假設你有一個“swal”工作。

我如何管理“確認”決定是:

.catch(swal.noop).then(function(result){...});

該“捕獲”指令主要用於后台單擊或模態關閉。 我認為它代表“無操作” ,但我不確定。 它適用於“OK”或“Not OK”按鈕。 它捕獲並殺死它。

因此,如果您實際上從兩個按鈕之一獲得“結果”,則應用“then”子句。
因此, result是布爾值true / false反映用戶的選擇。

你的代碼是:

function eliminarProducto(id){
  var url = '../php/apartado/elimina_producto.php';

  swal({
    title: '¡Atención!',
    text: "¿Desea eliminar el registro?",
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Aceptar'
  }).catch(swal.noop).then(function(result){  // Change here.
    if(result){                               // If "yes" from user.
      $.ajax({
        type:'POST',
        url:url,
        data:'id='+id,
        success: function(registro){
          $('#agrega-registros').html(registro);
        }
      });
    }
  })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM