javascript Jquery 和 Ajax 删除确认框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14657517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:15:36  来源:igfitidea点击:

Jquery and Ajax delete confirmation box

javascriptjqueryajax

提问by m1k1

i need help with this one. I have my function working well with ajax, but i need a ok / cancel delete confirmation box before i send ajax request in order to remove item from database. This is my code:

我需要这方面的帮助。我的功能与 ajax 配合良好,但在发送 ajax 请求以从数据库中删除项目之前,我需要一个确定/取消删除确认框。这是我的代码:

delete_article = function(article_id){
     $.ajax({
           type:"POST",
           url: "<?php echo base_url()?>admin/article/delete_article",
           data: "article_id="+article_id,
           asynchronous: true,
           cache: false,
           beforeSend: function(){

           },
          success: function(){
              $('#articletr'+article_id).hide();

          }

         });
    }
})

回答by Plynx

var answer = confirm ("Are you sure you want to delete from the database?");
if (answer)
{
     // your ajax code
}

If you want more control over these kinds of modal dialog prompts with JQuery (buttons that say something other than OK/cancel, more than two buttons, or different styles), you can use JQuery UI Modal Confirmation.

如果您想使用 JQuery 更好地控制这些类型的模态对话框提示(显示除 OK/cancel 以外的其他内容的按钮、两个以上的按钮或不同的样式),您可以使用JQuery UI Modal Confirmation

回答by bikeshedder

The simplest way is to use the builtin javascript method confirm:

最简单的方法是使用内置的 javascript 方法confirm

if (confirm("Really send?")) {
    // Do it!
}

Since those native browser dialogs have a very ugly history and can not be styled at all they are often avoided in favour of other methods. A great examples is the Dialog widgetof JQuery UI

由于那些本机浏览器对话框有着非常丑陋的历史并且根本无法设置样式,因此通常会避免使用其他方法。一个很好的例子是对话框控件JQuery用户界面