我正在尝试使用 jQuery UI 对话框在执行操作之前显示确认...在这种情况下导航到选定的链接...但在另一种情况下,我可能想使用 AJAX 删除。
我想我可以将操作作为 custom_confirm 函数的参数传递:
$("a.edit").click(function(e){
e.preventDefault();
custom_confirm('Please Note:',
function(){
location.href = $(this).attr('href');
}
);
});
function custom_confirm(prompt, action, title){
if (title === undefined) title = "Are you sure?";
if ($("#confirm").length == 0){
$("#main div.inner").append('<div id="confirm" title="' + title + '">' + prompt + '</div>');
$("#confirm").dialog({buttons: {'Proceed': function(){ $(this).dialog('close'); action; }, Cancel: function(){ $(this).dialog('close'); }}});
}
else {
$("#confirm").html(prompt);
$("#confirm").dialog('open');
}
}
它不起作用。还有其他方法可以实现吗?
感谢大家的快速回复。我尝试了你的建议,但它仍然没有执行作为参数传递的函数。
$("a.edit").click(function(e){
e.preventDefault();
var href = $(this).attr('href');
custom_confirm('Please Note:',
function(){
console.log(href);
location.href = href;
}
);
});
清理了 custom_confirm 函数,添加了关闭选项:
function custom_confirm(prompt, action, title){
if (title === undefined) title = "Are you sure?";
$("#main div.inner").append('<div id="confirm" title="' + title + '">' + prompt + '</div>');
$("#confirm").dialog({position: 'top', width: 700, modal: true, resizable: false, show: "fold", hide: "blind", buttons: {'Proceed': function(){ $(this).dialog('close'); action(); }, Cancel: function(){ $(this).dialog('close'); }}, close: function(ev, ui) { $(this).remove();}});
}
最佳答案
想通了。如果将一个函数作为参数传递给另一个函数,则需要将参数作为函数调用
action()
而不是作为变量
action
希望对你有帮助
$("a.edit").click(function(e){
e.preventDefault();
var href = $(this).attr('href');
custom_confirm('Please Note:',
function(){
location.href = href;
}
);
});
function custom_confirm(prompt, action, title){
if (title === undefined) title = "Are you sure?";
if ($("#confirm").length == 0){
$("#main div.inner").append('<div id="confirm" title="' + title + '">' + prompt + '</div>');
$("#confirm").dialog({buttons: {'Proceed': function(){ $(this).dialog('close'); action(); }, Cancel: function(){ $(this).dialog('close'); }}});
}
else {
$("#confirm").html(prompt);
$("#confirm").dialog('open');
}
}
关于javascript - jQuery UI 对话框确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/821067/