javascript 使用 jQuery 提供多个选项的警报

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

Alert with several option with jQuery

javascriptjquery

提问by Jennifer Anthony

I want make a alert that after click on button delete ask did you want delete this?with two options: okand cancel. If user clicks on okthe value is deleted. If the user clicks on canceldon't delete the value.

我想发出警报,在单击按钮删除后询问did you want delete this?两个选项:okcancel。如果用户点击ok该值被删除。如果用户点击cancel不要删除该值。

Like this in this site:

在这个网站上是这样的:

Stackoverflow confirmation dialog for post deletion vote

删除后投票的 Stackoverflow 确认对话框

How to do this with jQuery?

如何用 jQuery 做到这一点?

回答by Samich

<a href="#" id="delete">Delete</a>

$('#delete').click(function() {
   if (confirm('Do you want to delete this item?')) {
      // do delete item
   }
});

Code: http://jsfiddle.net/hP2Dz/4/

代码:http: //jsfiddle.net/hP2Dz/4/

回答by Ricardo Binns

If you want to style your alert, check this plugin: jquery-alert-dialogs. It's very easy to use.

如果要设置警报样式,请查看此插件:jquery-alert-dialogs。它非常容易使用。

jAlert('This is a custom alert box', 'Alert Dialog');

jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
     jAlert('Confirmed: ' + r, 'Confirmation Results');
});

jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r) {
    if( r ) alert('You entered ' + r);
});


UPDATE:the oficial site is currently offline. here is another source

更新:官方网站目前处于离线状态。这是另一个来源

回答by Jon Newmuis

In JavaScript, that type of box is confirm, not alert. confirmreturns a boolean, representing whether the user responded positively or negatively to the prompt (i.e. clicking OKresults in truebeing returned, whereas clicking cancelresults in falsebeing returned). This is applicable to jQuery, but also in JavaScript more broadly. You can say something like:

在 JavaScript 中,这种类型的框是confirm,而不是alertconfirm返回一个布尔值,表示用户对提示的响应是肯定的还是否定的(即单击OK导致true返回,而单击cancel导致false返回)。这适用于 jQuery,但也适用于更广泛的 JavaScript。你可以这样说:

var shouldDelete = confirm("Do you want to delete?");
if (shouldDelete) {
  // the user wants to delete
} else {
  // the user does not want to delete
}

回答by isJustMe

Might not be jquery but is the simple principle that you could use

可能不是 jquery,而是您可以使用的简单原则

<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("vote ");
if (r==true)
  {
  alert("ok delete"); //you can add your jquery here
  }
else
  {
  alert(" Cancel! dont delete"); //you can add your jquery here
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Vote to delete?" /> <!-- can be changed to object binding with jquery-->

</body>
</html>Vot