首页 > 解决方案 > 如何在 Ajax jQuery 中添加确认消息

问题描述

我需要确认盒。它应该显示消息“你想删除记录吗”。如果是,请删除。没有消息将被关闭,这是你做的简单的事情。我尝试了下面我附上的编码,请仔细阅读代码。当我运行代码时,不会显示任何输出。你能给它一个好的解决方案吗谢谢。

function RemoveTeam(id) {

 $.confirm({
    buttons: {
        hey: function () {



            $.ajax({
                type: 'POST',
                url: 'remove.php',
                dataType: 'JSON',
                data: {id: id},
                success: function (data) {


                    get_all();

                },

            error: function (xhr, status, error) {

            alert(xhr.responseText);
            //
            }

            });
            }, }
        }
    }

标签: phpjquery

解决方案


当我查看了您的 jquery 代码时,它可能会使用 jquery-confirm 插件,并且代码可能有助于解决您的问题。我使用虚拟 JSON 调用来生成 ajax 请求。

function removeTeam(id) {
  $.confirm({
    buttons: {
      hey: function() {
        $.ajax({
          url: 'https://craftpip.github.io/jquery-confirm/bower.json',
          dataType: 'json',
          method: 'get'
        }).done(function(response) {
          console.log(response);
        }).fail(function(err) {
          console.log('Something went wrong.');
        });
      },
      cancel: function() {
        console.log('the user clicked cancel');
      }
    }
  });
}
removeTeam(10);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>


推荐阅读