javascript - jQuery UI 对话框确认

标签 javascript jquery

我正在尝试使用 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/

相关文章:

javascript - Cordova - 拒绝执行内联事件处理程序,因为它违反了以下内容安全策略

jquery - 无法使用 jQuery :last 调整表格单元格的右边距

javascript - 如何让AngularJS默认选中一个复选框

javascript - Html5 原生日期选择器,有变化

javascript - 将字符串中的所有小数点替换为 ' point ' 并保持相同的数字

javascript - 禁用chrome的刷新图标

javascript - 在测试中完成提取时异步路径更新

javascript - 使用通过 jquery load 获取的内容?

javascript - jquery 模态不工作

jquery - 如何使用 jQuery 获取一个元素相对于另一个元素的位置?