如何添加 Jquery 确认模式

发布于 2024-12-22 13:27:06 字数 1076 浏览 0 评论 0原文

我试图使用 jquery 从数据库中删除数据,以下脚本帮助我做到了这一点。现在我想在最终删除它之前显示一个 jquery 确认模式(是:否)。我浏览了一些教程来学习如何做到这一点,但我无法让这些为我工作,

请告诉我如何添加确认模式。

谢谢

<script>

 $(function(){ // added
             $('a.delete').click(function(){
                var a_href = $(this).attr('href'); 
     $.ajax({

     type: "POST",
     url: "<?php echo base_url(); ?>batchlist/delete",
     data: "id="+a_href,
     success: function(server_response){

               if(server_response == '1')
                            { alert('MSG');}  else {$("#trid_"+a_href).hide('slow'); }

                                    }               

   });  //$.ajax ends here

                return false
    });//.click function ends here
  }); // function ends here         


 </script                                   

这是我的删除锚点

<a href="<?php echo $row['studentid']; ?>" title="Delete" class="delete" ><img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" /></a>            

I was trying to delete a data from database using jquery and the following script helped me to do so. Now I want to show a jquery confirmation modal(YES: NO) before it finally goes for deleting it. I went through some tutorials to learn how to do that but I couldn't make those work for me

Please kindly show me how to add the confirmation modal.

Thanks

<script>

 $(function(){ // added
             $('a.delete').click(function(){
                var a_href = $(this).attr('href'); 
     $.ajax({

     type: "POST",
     url: "<?php echo base_url(); ?>batchlist/delete",
     data: "id="+a_href,
     success: function(server_response){

               if(server_response == '1')
                            { alert('MSG');}  else {$("#trid_"+a_href).hide('slow'); }

                                    }               

   });  //$.ajax ends here

                return false
    });//.click function ends here
  }); // function ends here         


 </script                                   

This is my delete anchor

<a href="<?php echo $row['studentid']; ?>" title="Delete" class="delete" ><img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" /></a>            

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朮生 2024-12-29 13:27:06

你不需要 jQuery 来做到这一点。 Javascript 会很可爱地做到这一点:

if (confirm("Are you sure you want to contiune with this request?")) {
     //Do stuff
}

简单:)

you don't need jQuery to do this. Javascript will do this just lovelyly:

if (confirm("Are you sure you want to contiune with this request?")) {
     //Do stuff
}

Simple :)

靑春怀旧 2024-12-29 13:27:06

最简单的方法是使用基本的 javascript。

在 ajax 调用之前,将其放入

if(!confirm('Are you sure you want to delete this?') {
    return false;
}

So 如果他们拒绝,ajax 就不会打扰。

The simplest way to do this is with basic javascript.

Right before the ajax call, put this

if(!confirm('Are you sure you want to delete this?') {
    return false;
}

So if they decline, it just doesn't bother with the ajax.

兲鉂ぱ嘚淚 2024-12-29 13:27:06

我发现使用 jquery 小部件进行模式对话框并处理删除/取消按钮很方便。

(function ($) {
        $.widget('my.confirmDialog', {
            options: {
                url: null,
                elements: null,
                postParamName: 'id',
                afterDelete: null
            },
            _create: function () {
                var that = this;
                var options = that.options;
                that.element.dialog({
                    resizable: false,
                    autoOpen: false,
                    modal: true,
                    buttons: {
                        "Delete": function (e, ui) {
                            $(this).dialog("close");
                            $.post(options.url, that.postData, function (resp) {
                                that._trigger('afterDelete', e, resp);
                            });
                        },
                        "Cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                });
                options.elements.live('click', function (event, ui) {
                    that.askConfirmation(this);
                });
            },
            askConfirmation: function (askElem) {
                var that = this;

                var confirmationText = $(askElem).attr("data-confirmation");
                that.element.text(confirmationText);

                var postData = {};
                postData[that.options.postParamName] = $(askElem).attr("data-value");
                that.postData = postData;

                that.element.dialog("open");
            },
            destroy: function () {
                this.element.dialog("destroy");
                $.Widget.prototype.destroy.apply(this, arguments);
            }

        });

    } (jQuery));

用法非常简单,您只需创建用于确认对话框的 div:

<div title="Delete confirmation" id="sampleDeleteConfirmation"
    style="display: none;">
</div>

并将小部件应用于此 div,传递应触发确认对话框的元素显示:

    $(document).ready(function () {

        $("#sampleDeleteConfirmation").confirmDialog({
            url: '/DeleteUrl',
            postParamName: 'canChangeThisIfNeeded',
            elements: $(".confirmThis"),
            afterDelete: function (event, response) {
                //callback after delete
            }
        });

    });

也是为了方便起见,我在显示模式对话框的按钮上使用数据属性来修改文本并传递值对于帖子删除操作。

          <button type="button" class="confirmThis" data-value="123" data-confirmation="Delete item 123?">
                Delete
           </button>
          <button type="button" class="confirmThis" data-value="567" data-confirmation="Delete item 567?">
                Delete
           </button>

仅此而已)

I find it convinient to use jquery widget for modal dialog, and handle delete/cancel buttons.

(function ($) {
        $.widget('my.confirmDialog', {
            options: {
                url: null,
                elements: null,
                postParamName: 'id',
                afterDelete: null
            },
            _create: function () {
                var that = this;
                var options = that.options;
                that.element.dialog({
                    resizable: false,
                    autoOpen: false,
                    modal: true,
                    buttons: {
                        "Delete": function (e, ui) {
                            $(this).dialog("close");
                            $.post(options.url, that.postData, function (resp) {
                                that._trigger('afterDelete', e, resp);
                            });
                        },
                        "Cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                });
                options.elements.live('click', function (event, ui) {
                    that.askConfirmation(this);
                });
            },
            askConfirmation: function (askElem) {
                var that = this;

                var confirmationText = $(askElem).attr("data-confirmation");
                that.element.text(confirmationText);

                var postData = {};
                postData[that.options.postParamName] = $(askElem).attr("data-value");
                that.postData = postData;

                that.element.dialog("open");
            },
            destroy: function () {
                this.element.dialog("destroy");
                $.Widget.prototype.destroy.apply(this, arguments);
            }

        });

    } (jQuery));

usage is really simple, you just create div for confirmation dialog:

<div title="Delete confirmation" id="sampleDeleteConfirmation"
    style="display: none;">
</div>

and apply widget to this div, passing the elements that should trigger confirmation dialog show:

    $(document).ready(function () {

        $("#sampleDeleteConfirmation").confirmDialog({
            url: '/DeleteUrl',
            postParamName: 'canChangeThisIfNeeded',
            elements: $(".confirmThis"),
            afterDelete: function (event, response) {
                //callback after delete
            }
        });

    });

also for convinience i use data- attributes on buttons that show modal dialog to modify text and pass value for post to delete action.

          <button type="button" class="confirmThis" data-value="123" data-confirmation="Delete item 123?">
                Delete
           </button>
          <button type="button" class="confirmThis" data-value="567" data-confirmation="Delete item 567?">
                Delete
           </button>

and thats all )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文