はいまたはいいえjqueryを使用した確認ボックス
質問
OK/キャンセル]ボタンの代わりに、jQueryを使用してはい/いいえアラートが必要です。
jQuery.alerts.okButton = 'Yes';
jQuery.alerts.cancelButton = 'No';
jConfirm('Are you sure??', '', function(r) {
if (r == true) {
//Ok button pressed...
}
}
他の選択肢はありますか?
解決
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>
他のヒント
アラートメソッドは、ユーザーが閉じるまで実行をブロックします。
確認関数を使用します:
if (confirm('Some message')) {
alert('Thanks for confirming');
} else {
alert('Why did you press cancel? You should have confirmed');
}
これらのコードを使用しました:
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>
これらのコードは私にとっては機能しますが、これが適切かどうかは本当にわかりません。どう思いますか?
このjQueryプラグインをご覧ください。 jquery.confirm.
<a href="home" class="confirm">Go to home</a>
その後:
$(".confirm").confirm();
これにより、リンクを追跡する前に確認ポップアップが表示されます。
ここにデモがあります: http://myclabs.github.com/jquery.confirm/
私が見たすべての例は、さまざまな「はい/いいえ」というタイプの質問に対して再利用可能ではありません。コールバックを指定できるようにして、どんな状況にも電話をかけることができるようなものを探していました。
以下は私にとってうまく機能しています:
$.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");
}
});
それから私はそれをこのように呼びます:
var deleteOk = function() {
uploadFile.del(fileid, function() {alert("Deleted")})
};
$.confirm(
"CONFIRM", //title
"Delete " + filename + "?", //message
"Delete", //button text
deleteOk //"yes" callback
);
ダイアログボックスから答えを取り戻すのに苦労しましたが、最終的にはこの他の質問からの答えを組み合わせることで解決策を思いつきました display-and-no-buttons-instead-of-ok and-cancel-in-confirm-box モーダル確認ダイアログのコードの一部を使用します
これは、他の質問に対して提案されたものです。
独自の確認ボックスを作成します:
<div id="confirmBox">
<div class="message"></div>
<span class="yes">Yes</span>
<span class="no">No</span>
</div>
自分で作る 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();
}
あなたのコードでそれを呼び出します:
doConfirm("Are you sure?", function yes()
{
form.submit();
}, function no()
{
// do nothing
});
私の変更私は上記を微調整して、電話する代わりに 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();
}
}
});
私が行った他の変更は、Thulasiramが彼の答えで行ったように、Doconfirm関数内にCondienbox Divを作成することでした。
OKボタンとキャンセルボタンに翻訳を適用する必要がありました。ダイナミックテキストを除いてコードを変更しました(翻訳関数を呼び出します)
$.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);
}
});
あなたはあなたの確認を再利用することができます:
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);