Ja oder Nein bestätigen Box mit jQuery
Frage
Ich will ja / keine Warnungen jQuery verwenden, statt ok / Abbrechen-Taste:
jQuery.alerts.okButton = 'Yes';
jQuery.alerts.cancelButton = 'No';
jConfirm('Are you sure??', '', function(r) {
if (r == true) {
//Ok button pressed...
}
}
Alle anderen Alternativen?
Lösung
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>
Andere Tipps
Die Alarm Methode blockiert die Ausführung, bis der Benutzer schließt es:
Mit der Bestätigungsfunktion:
if (confirm('Some message')) {
alert('Thanks for confirming');
} else {
alert('Why did you press cancel? You should have confirmed');
}
Ich habe diese Codes verwendet:
HTML:
<a id="delete-button">Delete</a>
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>
arbeiten Diese Codes für mich, aber ich bin nicht wirklich sicher, ob dies richtig ist. Was denken Sie?
Haben Sie einen Blick auf diese jQuery-Plugin. jquery.confirm
<a href="home" class="confirm">Go to home</a>
und dann:
$(".confirm").confirm();
Dies wird ein Bestätigungsfenster anzuzeigen, bevor Sie den Link fortfahren.
Es gibt eine Demo hier: http://myclabs.github.com/jquery.confirm/
das Beispiel, das ich gesehen habe, sind nicht wiederverwendbar für andere „ja / nein“ Art Fragen. Ich war auf der Suche nach etwas, das mir einen Rückruf angeben erlauben würde, so dass ich für jede Situation nennen könnte.
In der folgenden gut funktioniert für mich:
$.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");
}
});
ich es dann rufen Sie wie folgt aus:
var deleteOk = function() {
uploadFile.del(fileid, function() {alert("Deleted")})
};
$.confirm(
"CONFIRM", //title
"Delete " + filename + "?", //message
"Delete", //button text
deleteOk //"yes" callback
);
Ich hatte Mühe, die Antwort zurück aus dem Dialogfeld, aber schließlich kam mit einer Lösung, indem die Antwort von dieser anderen Frage Kombination
Ich brauche eine Übersetzung Ok anwenden und Schaltflächen Abbrechen. Ich den Code geändert dynamischen Text auszunehmen (ruft meine Übersetzungsfunktion)
$.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);
}
});
Sie können bestätigen verwendet werden können:
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);