-1

我可以向 jQuery“警报”添加按钮吗?我想像确认框一样使用警告框。我需要在警报上有 2 个按钮,如果用户单击是,一个人的数据将被删除。如果单击否,则警报将关闭。

4

2 回答 2

7

alert()是一个 JavaScript 函数,与 jQuery无关。

您可能需要confirm()提示(同样是 JavaScript,而不是 jQuery)。

var result = confirm("Are you sure you want to delete this user?");

if (result) {
    // Delete the user
} else {
    // Do nothing; they cancelled
}

对于更高级的弹出窗口,您可以使用“模型窗口”模拟您自己的弹出窗口;如果您在 Internet 上搜索,则存在很多。

于 2012-09-28T09:05:40.043 回答
3

使用“JQuery UI”(使用另一个名为 JQuery 的库用 javascript 编写的库),您可以使用以下代码:

    $('body').append('<div id="yesno_dialog" title="Yes Or No"><p>Do you wish to Yes or to No</p></div>');
    $("#yesno_dialog").dialog({
        title: "Yes or No",
        resizable: false,
        modal: true,
        buttons: {
            "Yes" : function () {
                alert("You chose yes.. now let's do something else here");
                $(this).dialog("close");
                $(this).remove();
            },
            "No" : function (){
                alert("You chose no.. now let's do something else here");
                $(this).dialog("close");
                $(this).remove();
            }

        }
    });
}  

但是您可能只需要从确认返回值

    return confirm("Are you sure");
于 2012-09-28T10:03:39.680 回答