使 javascript 警报是/否而不是确定/取消

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11046572/
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-08-24 04:13:21  来源:igfitidea点击:

Make javascript alert Yes/No Instead of Ok/Cancel

javascript

提问by

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" />

How to create a yes/no/cancel alert box instead of ok/cancel alert box in Javascript?

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

采纳答案by Nikhil D

You can use jQuery UI Dialog.

您可以使用jQuery UI 对话框

These libraries create HTML elements that look and behave like a dialog box, allowing you to put anything you want (including form elements or video) in the dialog.

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

回答by FredFury

In an attempt to solve a similar situation I've come across thisexample and adaptedit. It uses JQUERY UI Dialogas Nikhil D suggested. Here is a look at the code:

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

HTML:

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:

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

I have a few more tweaks I need to do to make this example work for my application. Will update this if I see it fit into the answer. Hope this helps someone.

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

回答by FredFury

You cannot do that with the native confirm()as it is the browser's method.

您不能使用本机来做到这一点,confirm()因为它是浏览器的方法。

You have to create a plugin for a confirm box (or try one created by someone else). And they often look better, too.

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

Additional Tip: Change your English sentence to something like

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

Really, Delete this Thing?

真的,删除这个东西?

回答by Nikhil D

"Confirm" in Javascript stops the whole process until it gets a mouse response on its buttons. If that is what you are looking for, you can refer jquery-ui but if you have nothing running behind your process while receiving the response and you control the flow programatically, take a look at this. You will have to hard-code everything by yourself but you have complete command over customization. https://www.w3schools.com/howto/howto_css_modals.asp

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

回答by user3500176

I shall try the solution with jQuery, for sure it should give a nice result. Of course you have to load jQuery ... What about a pop-up with something like this? Of course this is dependant on the user authorizing pop-ups.

我将尝试使用 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>