38
function btnSelete_Click() {
    var strconfirm = confirm("Are you sure you want to delete?");
    if (strconfirm == true) {
        return true;
    }
}

<asp:Button ID="btnSelect" runat="server" Text="Select" Onclientclick="return btnSelete_Click();" CssClass="cssbutton" />

如何在Javascript中创建一个是/否/取消警报框而不是确定/取消警报框?

4

6 回答 6

11

为了解决类似的情况,我遇到了这个例子并对其进行了调整。它使用Nikhil D 建议的JQUERY UI 对话框。下面看一下代码:

HTML:

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<input type="button" id="box" value="Confirm the thing?" />
<div id="dialog-confirm"></div>

JavaScript:

$('#box').click(function buttonAction() {
  $("#dialog-confirm").html("Do you want to do the thing?");

  // Define the Dialog and its properties.
  $("#dialog-confirm").dialog({
    resizable: false,
    modal: true,
    title: "Do the thing?",
    height: 250,
    width: 400,
    buttons: {
      "Yes": function() {
        $(this).dialog('close');
        alert("Yes, do the thing");
      },
      "No": function() {
        $(this).dialog('close');
        alert("Nope, don't do the thing");
      }
    }
  });
});

$('#box').click(buttonAction);

为了使这个示例适用于我的应用程序,我还需要做一些调整。如果我认为它适合答案,将更新它。希望这可以帮助某人。

于 2015-12-31T10:03:24.540 回答
7

您不能使用本机执行此操作,confirm()因为它是浏览器的方法。

您必须为确认框创建一个插件(或尝试其他人创建的插件)。而且它们通常看起来也更好。

附加提示:将您的英语句子更改为类似

真的,删除这个东西?

于 2012-06-15T07:46:33.007 回答
4

您可以使用jQuery UI 对话框

这些库创建的 HTML 元素的外观和行为类似于对话框,允许您在对话框中放置任何您想要的内容(包括表单元素或视频)。

于 2012-06-15T07:49:45.580 回答
2

构建了一个微小的、类似确认的 vanilla js 是/否对话框。
https://www.npmjs.com/package/yesno-dialog

在此处输入图像描述

于 2020-08-20T22:34:54.903 回答
1

我将尝试使用 jQuery 的解决方案,它肯定会给出一个不错的结果。当然,你必须加载 jQuery ...... 弹出这样的东西怎么样?当然,这取决于用户授权弹出窗口。

<html>
    <head>
    <script language="javascript">
    var ret;
    function returnfunction()
    {
        alert(ret);
    }
    </script>
</head>
    <body>
        <form>
            <label id="QuestionToAsk" name="QuestionToAsk">Here is talked.</label><br />
            <input type="button" value="Yes" name="yes" onClick="ret=true;returnfunction()" />
            <input type="button" value="No" onClick="ret=false;returnfunction()" />
        </form>
    </body>
</html>
于 2015-12-22T20:15:19.547 回答
1

Javascript 中的“确认”会停止整个过程,直到它的按钮得到鼠标响应。如果这是您正在寻找的,您可以参考 jquery-ui 但如果您在接收响应时没有在您的进程后面运行任何东西并且您以编程方式控制流程,请看一下这个。您必须自己对所有内容进行硬编码,但您可以完全控制自定义。https://www.w3schools.com/howto/howto_css_modals.asp

于 2018-03-01T13:13:58.573 回答