【问题标题】:Yes or No confirm box using jQuery使用 jQuery 是或否确认框
【发布时间】:2010-08-19 08:10:30
【问题描述】:

我想要使用 jQuery 的是/否警报,而不是确定/取消按钮:

jQuery.alerts.okButton = 'Yes';
jQuery.alerts.cancelButton = 'No';                  
jConfirm('Are you sure??',  '', function(r) {
    if (r == true) {                    
        //Ok button pressed...
    }  
}

还有其他选择吗?

【问题讨论】:

标签: jquery alerts jconfirm


【解决方案1】:

ConfirmDialog('Are you sure');

function ConfirmDialog(message) {
  $('<div></div>').appendTo('body')
    .html('<div><h6>' + message + '?</h6></div>')
    .dialog({
      modal: true,
      title: 'Delete message',
      zIndex: 10000,
      autoOpen: true,
      width: 'auto',
      resizable: false,
      buttons: {
        Yes: function() {
          // $(obj).removeAttr('onclick');                                
          // $(obj).parents('.Parent').remove();

          $('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');

          $(this).dialog("close");
        },
        No: function() {
          $('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');

          $(this).dialog("close");
        }
      },
      close: function(event, ui) {
        $(this).remove();
      }
    });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

【讨论】:

  • 这是 jquery-ui 还是普通的旧 jquery?我在 jQuery 文档的任何地方都没有看到 .dialog()。
  • 你需要在你的页面中包含 jquery 和 jquery ui 脚本。
  • @Thulasiram 如何在yii2 框架中添加插件?
【解决方案2】:

alert 方法会阻止执行,直到用户关闭它:

使用确认功能:

if (confirm('Some message')) {
    alert('Thanks for confirming');
} else {
    alert('Why did you press cancel? You should have confirmed');
}

【讨论】:

  • confirm 是“OK CANCEL”,而不是“YES NO”。
【解决方案3】:

我用过这些代码:

HTML:

<a id="delete-button">Delete</a>

jQuery:

<script>
$("#delete-button").click(function(){
    if(confirm("Are you sure you want to delete this?")){
        $("#delete-button").attr("href", "query.php?ACTION=delete&ID='1'");
    }
    else{
        return false;
    }
});
</script>

这些代码对我有用,但我不确定这是否正确。你怎么看?

【讨论】:

  • 更短的return confirm("Are you sure you want to return this meter?") ))
【解决方案4】:

看看这个 jQuery 插件:jquery.confirm

<a href="home" class="confirm">Go to home</a>

然后:

$(".confirm").confirm();

这将在继续点击链接之前显示一个确认弹出窗口。

这里有一个演示:http://myclabs.github.com/jquery.confirm/

【讨论】:

    【解决方案5】:

    我看到的所有示例都不能用于不同的“是/否”类型的问题。我正在寻找可以让我指定回调的东西,以便我可以在任何情况下调用。

    以下对我来说效果很好:

    $.extend({ confirm: function (title, message, yesText, yesCallback) {
        $("<div></div>").dialog( {
            buttons: [{
                text: yesText,
                click: function() {
                    yesCallback();
                    $( this ).remove();
                }
            },
            {
                text: "Cancel",
                click: function() {
                    $( this ).remove();
                }
            }
            ],
            close: function (event, ui) { $(this).remove(); },
            resizable: false,
            title: title,
            modal: true
        }).text(message).parent().addClass("alert");
    }
    });
    

    然后我这样称呼它:

    var deleteOk = function() {
        uploadFile.del(fileid, function() {alert("Deleted")})
    };
    
    $.confirm(
        "CONFIRM", //title
        "Delete " + filename + "?", //message
        "Delete", //button text
        deleteOk //"yes" callback
    );
    

    【讨论】:

      【解决方案6】:

      我无法从对话框中获取答案,但最终通过将另一个问题 display-yes-and-no-buttons-instead-of-ok-and-cancel-in-confirm-box 的答案与模态确认对话框中的部分代码结合起来找到了解决方案

      这是对另一个问题的建议:

      创建您自己的确认框:

      <div id="confirmBox">
          <div class="message"></div>
          <span class="yes">Yes</span>
          <span class="no">No</span>
      </div>
      

      创建自己的confirm() 方法:

      function doConfirm(msg, yesFn, noFn)
      {
          var confirmBox = $("#confirmBox");
          confirmBox.find(".message").text(msg);
          confirmBox.find(".yes,.no").unbind().click(function()
          {
              confirmBox.hide();
          });
          confirmBox.find(".yes").click(yesFn);
          confirmBox.find(".no").click(noFn);
          confirmBox.show();
      }
      

      通过您的代码调用它:

      doConfirm("Are you sure?", function yes()
      {
          form.submit();
      }, function no()
      {
          // do nothing
      });
      

      我的更改 我已经调整了上述内容,而不是调用confirmBox.show(),而是像这样使用confirmBox.dialog({...})

      confirmBox.dialog
          ({
            autoOpen: true,
            modal: true,
            buttons:
              {
                'Yes': function () {
                  $(this).dialog('close');
                  $(this).find(".yes").click();
                },
                'No': function () {
                  $(this).dialog('close');
                  $(this).find(".no").click();
                }
              }
          });
      

      我所做的另一项更改是在 doConfirm 函数中创建 confirmBox div,就像 ThulasiRam 在他的回答中所做的那样。

      【讨论】:

        【解决方案7】:

        我需要对“确定”和“取消”按钮进行翻译。我将代码修改为动态文本除外(调用我的翻译函数)


        $.extend({
            confirm: function(message, title, okAction) {
                $("<div></div>").dialog({
                    // Remove the closing 'X' from the dialog
                    open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                    width: 500,
                    buttons: [{
                        text: localizationInstance.translate("Ok"),
                        click: function () {
                            $(this).dialog("close");
                            okAction();
                        }
                    },
                        {
                        text: localizationInstance.translate("Cancel"),
                        click: function() {
                            $(this).dialog("close");
                        }
                    }],
                    close: function(event, ui) { $(this).remove(); },
                    resizable: false,
                    title: title,
                    modal: true
                }).text(message);
            }
        });

        【讨论】:

          【解决方案8】:

          试试这个...这很简单,只需使用确认对话框进行警报 YES|NO。

          if(confirm("您要升级吗?")){ 你的代码 }

          【讨论】:

            【解决方案9】:

            您可以重复使用您的确认:

                function doConfirm(body, $_nombrefuncion)
                {   var param = undefined;
                    var $confirm = $("<div id='confirm' class='hide'></div>").dialog({
                        autoOpen: false,
                        buttons: {
                            Yes: function() {
                            param = true;
                            $_nombrefuncion(param);
                            $(this).dialog('close');
                        },
                            No: function() {
                            param = false;
                            $_nombrefuncion(param);
                            $(this).dialog('close');
                        }
                                                }
                    });
                    $confirm.html("<h3>"+body+"<h3>");
                    $confirm.dialog('open');                                     
                };
            
            // for this form just u must change or create a new function for to reuse the confirm
                function resultadoconfirmresetVTyBFD(param){
                    $fecha = $("#asigfecha").val();
                    if(param ==true){
                          // DO THE CONFIRM
                    }
                }
            
            //Now just u must call the function doConfirm
                doConfirm('body message',resultadoconfirmresetVTyBFD);
            

            【讨论】:

            • 请使用正确的英文,避免使用“u”之类的缩写。此外,如果您能解释一下您的代码以说明和教育 OP,以及为什么您认为您的答案比以前发布的任何内容都好,那就更好了。
            猜你喜欢
            • 2010-10-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-02
            • 2012-02-09
            • 2010-10-04
            • 1970-01-01
            • 2020-01-17
            相关资源
            最近更新 更多