是或否使用jQuery确认框

时间:2010-08-19 08:10:30

标签: jquery alerts jconfirm

我想使用jQuery的是/否警报,而不是ok / Cancel按钮:

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

还有其他选择吗?

9 个答案:

答案 0 :(得分:116)

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();
        }
    });
};

请找到Demo

答案 1 :(得分:98)

警告方法阻止执行直到用户关闭它:

使用确认功能:

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

答案 2 :(得分:46)

我已使用这些代码:

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>

这些代码对我有用,但我不确定这是否合适。你觉得怎么样?

答案 3 :(得分:28)

看看这个jQuery插件:jquery.confirm

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

然后:

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

这会在继续关注链接之前显示确认弹出窗口。

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

答案 4 :(得分:10)

我见过的所有例子都不能用于不同的“是/否”类型问题。我正在寻找能够指定回调的东西,所以我可以调用任何情况。

以下对我来说效果很好:

$.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
);

答案 5 :(得分:2)

我无法从对话框中获得答案,但最终提出了一个解决方案,将另一个问题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在他的回答中所做的那样。

答案 6 :(得分:0)

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

$.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);
    }
});

答案 7 :(得分:0)

尝试一下...这很简单,只需使用“是”或“否”的提示使用“确认”对话框即可。

if(confirm(“您要升级吗?”)){ 您的密码 }

答案 8 :(得分:-3)

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

    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);
相关问题