简体   繁体   English

甜蜜警报确认 - 获取用户响应

[英]Sweet Alert Confirm - Get user response

I have a JS file where I have a function like this: 我有一个JS文件,我有这样的函数:

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

The message showed in the browser looks simple, so I want to change it by an Sweet Alert message... But I don't know how! 浏览器中显示的消息看起来很简单,所以我想通过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;
}

*********************** EDITED POST ****************************** NEW CODE ***********************编辑后************************* *****新代码

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);
      }
    });
  })
}

How can I break the action in case if I select the option cancel??? 如果我选择取消选项,我怎么能打破行动??? :) :)

Assuming you have a "swal" working. 假设你有一个“swal”工作。

How I manage the "confirm" decision is: 我如何管理“确认”决定是:

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

That "catch" instruction is for the background click OR modal close, mainly. 该“捕获”指令主要用于后台单击或模态关闭。 I think it stands for "No Operation" , but I'm unsure. 我认为它代表“无操作” ,但我不确定。 It is for whatever else the "OK" or "Not OK" buttons. 它适用于“OK”或“Not OK”按钮。 It catches that and kills it. 它捕获并杀死它。

So if you actually have a "result" from one of the two buttons, the "then" clause applies. 因此,如果您实际上从两个按钮之一获得“结果”,则应用“then”子句。
So result is a boolean true/false reflecting user's choice. 因此, result是布尔值true / false反映用户的选择。

Your code would be: 你的代码是:

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