【问题标题】:bootstrap button loading with ajax使用ajax加载引导按钮
【发布时间】:2016-07-24 21:20:08
【问题描述】:

我正在尝试使用 ajax 删除 mysql 的记录。一切正常。但是当我单击垃圾按钮时,我还想向按钮添加加载。下面是我的按钮链接

<a href="#" id="delpost" data-id="'.$row['id'].'" class="btn btn-default pull-right" title="Delete"><i class="fa fa-trash-o"></i></a>

下面是我的ajax

$(function() {
    $('body').on('click', '#delpost', function() {
        var del_id = $(this).attr("data-id");
        var info = 'id=' + del_id;
        var parent = $(this).closest('li');
        var btn = $(this);
        btn.button('loading');
        if (confirm("Sure you want to delete this Post? There is NO undo!")) {
            $.ajax({
                type: "POST",
                url: "/delete-post.php",
                data: info,
                beforeSend: function() {
                    parent.animate({
                        'backgroundColor': '#fb6c6c'
                    }, 300);
                },
                success: function() {
                    btn.button('reset');
                    parent.slideUp(300, function() {
                        parent.remove();
                    });
                }
            });
        }
        return false;
    });
});

【问题讨论】:

  • 抱歉,问题出在哪里?
  • 你可以使用类似this

标签: php jquery ajax twitter-bootstrap


【解决方案1】:

这是简单的 jQuery 解决方案,与 bootstrap 无关

你可以添加一个loading.gif的隐藏图片,你可以从谷歌下载它并调整它的css,使它与你的删除链接出现在同一行:

<a href="#" id="delpost" data-id="'.$row['id'].'" class="btn btn-default pull-right" title="Delete"><i class="fa fa-trash-o"></i></a> <img src="path/to/your/loading.gif" style="display:none;" alt="loading" id="loading" />

并修改您的 jquery 代码如下:

$(function() {
        $('body').on('click','#delpost',function(){
            var del_id = $(this).attr("data-id");
            var info = 'id=' + del_id;
            var parent = $(this).closest('li');
            var btn=$(this);
            btn.button('loading');
            if(confirm("Sure you want to delete this Post? There is NO undo!"))
            {
                $.ajax({
                    type: "POST",
                    url: "/delete-post.php",
                    data: info,
                    beforeSend: function() {
                        parent.animate({'backgroundColor':'#fb6c6c'},300);
                        $('#loading').show();//shows loading gif image
                    },
                    success: function(){
                        $('#loading').hide();//hides loading gif image
                        btn.button('reset');
                        parent.slideUp(300,function() {
                            parent.remove();
                        });
                    }
                });
            }
            return false;
        });
});

【讨论】:

  • @AseshaGeorge 你的萤火虫控制台有什么错误吗?
  • @AseshaGeorge,你可以在firefox浏览器的firebug中打开你的控制台并输入jQuery('#loading');查看它是否返回一个对象,并且在该对象悬停时,它应该突出显示您隐藏的 loading.gif 图像,因此您需要显示它:jQuery('#loading').show();
  • 内容从另一个页面加载,图像链接在另一个页面上,ajax 在索引页面上。有问题吗?
  • 您需要添加我在链接后发布的图像标签(锚标签),还需要修改您的 ajax 脚本以在发送前显示此图像并在 ajax 成功时隐藏它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 2014-11-27
  • 1970-01-01
  • 2021-04-10
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多