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
Alert with several option with jQuery
提问by Jennifer Anthony
I want make a alert that after click on button delete ask did you want delete this?
with two options: ok
and cancel
. If user clicks on ok
the value is deleted. If the user clicks on cancel
don't delete the value.
我想发出警报,在单击按钮删除后询问did you want delete this?
两个选项:ok
和cancel
。如果用户点击ok
该值被删除。如果用户点击cancel
不要删除该值。
Like this in this site:
在这个网站上是这样的:
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
}
});
回答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
. confirm
returns a boolean, representing whether the user responded positively or negatively to the prompt (i.e. clicking OK
results in true
being returned, whereas clicking cancel
results in false
being returned). This is applicable to jQuery, but also in JavaScript more broadly. You can say something like:
在 JavaScript 中,这种类型的框是confirm
,而不是alert
。 confirm
返回一个布尔值,表示用户对提示的响应是肯定的还是否定的(即单击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