是或否使用 jQuery 的确认框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3519861/
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
Yes or No confirm box using jQuery
提问by Sushanth CS
I want yes/No alerts using jQuery, instead of ok/Cancel button:
我想要使用 jQuery 是/否警报,而不是确定/取消按钮:
jQuery.alerts.okButton = 'Yes';
jQuery.alerts.cancelButton = 'No';
jConfirm('Are you sure??', '', function(r) {
if (r == true) {
//Ok button pressed...
}
}
Any other alternatives?
还有其他选择吗?
回答by Thulasiram
ConfirmDialog('Are you sure');
function ConfirmDialog(message) {
$('<div></div>').appendTo('body')
.html('<div><h6>' + message + '?</h6></div>')
.dialog({
modal: true,
title: 'Delete message',
zIndex: 10000,
autoOpen: true,
width: 'auto',
resizable: false,
buttons: {
Yes: function() {
// $(obj).removeAttr('onclick');
// $(obj).parents('.Parent').remove();
$('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');
$(this).dialog("close");
},
No: function() {
$('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');
$(this).dialog("close");
}
},
close: function(event, ui) {
$(this).remove();
}
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
回答by Ana El Bembo
The alert method blocks execution until the user closes it:
alert 方法会阻止执行,直到用户关闭它:
use the confirm function:
使用确认功能:
if (confirm('Some message')) {
alert('Thanks for confirming');
} else {
alert('Why did you press cancel? You should have confirmed');
}
回答by user3678239
I've used these codes:
我用过这些代码:
HTML:
HTML:
<a id="delete-button">Delete</a>
jQuery:
jQuery:
<script>
$("#delete-button").click(function(){
if(confirm("Are you sure you want to delete this?")){
$("#delete-button").attr("href", "query.php?ACTION=delete&ID='1'");
}
else{
return false;
}
});
</script>
These codes works for me, but I'm not really sure if this is proper. What do you think?
这些代码对我有用,但我不确定这是否合适。你怎么认为?
回答by Matthieu Napoli
Have a look at this jQuery plugin: jquery.confirm.
看看这个 jQuery 插件:jquery.confirm。
<a href="home" class="confirm">Go to home</a>
and then:
进而:
$(".confirm").confirm();
This will show a confirmation popup before proceeding to following the link.
在继续点击链接之前,这将显示一个确认弹出窗口。
There's a demo here: http://myclabs.github.com/jquery.confirm/
这里有一个演示:http: //myclas.github.com/jquery.confirm/
回答by MSquared
All the example I've seen aren't reusable for different "yes/no" type questions. I was looking for something that would allow me to specify a callback so I could call for any situation.
我看到的所有示例都不能用于不同的“是/否”类型的问题。我正在寻找可以让我指定回调的东西,以便我可以在任何情况下调用。
The following is working well for me:
以下对我来说效果很好:
$.extend({ confirm: function (title, message, yesText, yesCallback) {
$("<div></div>").dialog( {
buttons: [{
text: yesText,
click: function() {
yesCallback();
$( this ).remove();
}
},
{
text: "Cancel",
click: function() {
$( this ).remove();
}
}
],
close: function (event, ui) { $(this).remove(); },
resizable: false,
title: title,
modal: true
}).text(message).parent().addClass("alert");
}
});
I then call it like this:
然后我这样称呼它:
var deleteOk = function() {
uploadFile.del(fileid, function() {alert("Deleted")})
};
$.confirm(
"CONFIRM", //title
"Delete " + filename + "?", //message
"Delete", //button text
deleteOk //"yes" callback
);
回答by J F
I had trouble getting the answer back from the dialog box but eventually came up with a solution by combining the answer from this other question display-yes-and-no-buttons-instead-of-ok-and-cancel-in-confirm-boxwith part of the code from the modal-confirmation dialog
我无法从对话框中获得答案,但最终通过结合其他问题的答案提出了一个解决方案display-yes-and-no-buttons-instead-of-ok-and-cancel-in-confirm-盒子从模式,确认对话框部分的代码
This is what was suggested for the other question:
这是对另一个问题的建议:
Create your own confirm box:
创建您自己的确认框:
<div id="confirmBox">
<div class="message"></div>
<span class="yes">Yes</span>
<span class="no">No</span>
</div>
Create your own confirm()
method:
创建自己的confirm()
方法:
function doConfirm(msg, yesFn, noFn)
{
var confirmBox = $("#confirmBox");
confirmBox.find(".message").text(msg);
confirmBox.find(".yes,.no").unbind().click(function()
{
confirmBox.hide();
});
confirmBox.find(".yes").click(yesFn);
confirmBox.find(".no").click(noFn);
confirmBox.show();
}
Call it by your code:
通过您的代码调用它:
doConfirm("Are you sure?", function yes()
{
form.submit();
}, function no()
{
// do nothing
});
MY CHANGESI have tweaked the above so that instead of calling confirmBox.show()
I used confirmBox.dialog({...})
like this
我的变化我已经调整了上面的内容,而不是 像这样调用confirmBox.show()
我confirmBox.dialog({...})
confirmBox.dialog
({
autoOpen: true,
modal: true,
buttons:
{
'Yes': function () {
$(this).dialog('close');
$(this).find(".yes").click();
},
'No': function () {
$(this).dialog('close');
$(this).find(".no").click();
}
}
});
The other change I made was to create the confirmBox div within the doConfirm function, like ThulasiRam did in his answer.
我所做的另一个更改是在 doConfirm 函数中创建 confirmBox div,就像 ThulasiRam 在他的回答中所做的那样。
回答by Jim
I needed to apply a translation to the Ok and Cancel buttons. I modified the code to except dynamic text (calls my translation function)
我需要对 Ok 和 Cancel 按钮应用翻译。我将代码修改为动态文本除外(调用我的翻译函数)
$.extend({
confirm: function(message, title, okAction) {
$("<div></div>").dialog({
// Remove the closing 'X' from the dialog
open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
width: 500,
buttons: [{
text: localizationInstance.translate("Ok"),
click: function () {
$(this).dialog("close");
okAction();
}
},
{
text: localizationInstance.translate("Cancel"),
click: function() {
$(this).dialog("close");
}
}],
close: function(event, ui) { $(this).remove(); },
resizable: false,
title: title,
modal: true
}).text(message);
}
});
回答by thamaht
You can reuse your confirm:
您可以重复使用您的确认:
function doConfirm(body, $_nombrefuncion)
{ var param = undefined;
var $confirm = $("<div id='confirm' class='hide'></div>").dialog({
autoOpen: false,
buttons: {
Yes: function() {
param = true;
$_nombrefuncion(param);
$(this).dialog('close');
},
No: function() {
param = false;
$_nombrefuncion(param);
$(this).dialog('close');
}
}
});
$confirm.html("<h3>"+body+"<h3>");
$confirm.dialog('open');
};
// for this form just u must change or create a new function for to reuse the confirm
function resultadoconfirmresetVTyBFD(param){
$fecha = $("#asigfecha").val();
if(param ==true){
// DO THE CONFIRM
}
}
//Now just u must call the function doConfirm
doConfirm('body message',resultadoconfirmresetVTyBFD);