带有 3 个按钮的 JavaScript 警报

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

JavaScript alert with 3 buttons

javascript

提问by Brian

I have a JavaScript alert button that appears on the website when a user is going to proceed to a signup page.

我有一个 JavaScript 警报按钮,当用户要进入注册页面时,该按钮会出现在网站上。

It looks something like this

它看起来像这样

Would you like to proceed to the signup page?
< OK > < Cancel >

您想进入注册页面吗?
<确定> <取消>

Would it be possible to add a third button to the alert so that it looks like

是否可以在警报中添加第三个按钮,使其看起来像

Would you like to proceed to the signup page?
< More Info > < OK > < Cancel >

您想进入注册页面吗?
<更多信息> <确定> <取消>

回答by SLaks

No.

不。

You need to use a custom modal dialog, such as jQuery UI Dialog.

您需要使用自定义模态对话框,例如jQuery UI Dialog

回答by hfarazm

however you can use confirm function.. but NOT three buttons.

但是你可以使用确认功能......但不是三个按钮。

var r=confirm("Press a button!");
if (r==true)
  {
  x="You pressed OK!";
  }
 else
  {
  x="You pressed Cancel!";
  }

回答by gengns

Nowdays, you can use an HTML dialogelement.

现在,您可以使用 HTMLdialog元素。

<dialog open>
  <p>Greetings, one and all!</p>
  <button>Ok</button><button>Maybe</button><button>Cancel</button>
</dialog>

And do what you want with it.

用它做你想做的事。

document.querySelectorAll('button').forEach($button => 
  $button.onclick = () => document.querySelector('dialog').removeAttribute('open'))

https://jsfiddle.net/6nus2zt4/1/

https://jsfiddle.net/6nus2zt4/1/

Hope this help to future generations :)

希望这对后代有所帮助:)

回答by mwgriffith

Unfortunately, I don't think you can change the number of buttons in an alert box. (At least not in any browser that I know of.) You can use one of the many modal dialog scripts that are out on the net. Something like Eric Martin's SimpleModal Dialogfor jquery would probably work.

不幸的是,我认为您无法更改警报框中的按钮数量。(至少在我所知道的任何浏览器中都没有。)您可以使用网络上的众多模态对话框脚本之一。像 Eric Martin's SimpleModal Dialogfor jquery 这样的东西可能会起作用。

They basically take a div and style it up with css and javascript to mimic a dialog box.

他们基本上采用 div 并使用 css 和 javascript 对其进行样式设置以模仿对话框。