Подтверждение диалога jQuery UI - PullRequest
2 голосов
/ 04 мая 2009

Я пытаюсь использовать диалоговое окно 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();}});
}

Ответы [ 3 ]

2 голосов
/ 04 мая 2009

Разобрался. Если вы передаете функцию в качестве параметра другой функции, вам необходимо вызвать параметр как функцию

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');
    }
}
0 голосов
/ 04 мая 2009

Да, или просто:

$("a.edit").click(function(e){
       e.preventDefault();
       var href = $(this).attr('href');
       custom_confirm('Please Note:',
           function(){
               location.href = href;
           }
       );
   });
0 голосов
/ 04 мая 2009

Вам необходимо передать переменную this с закрытием.

См. Этот пример. (улучшено)

   $("a.edit").click(function(e){
       e.preventDefault();
       custom_confirm('Please Note:',
           (function (element) {
             return function(element){
               location.href = $(element).attr('href');
           })(this)
       );
   });

Но то, что вы пытаетесь сделать, может быть сделано следующим образом:

$("a.edit").each(function() {
  $(this).click(function() {
    if (!confirm("Are you sure about that?"))
       e.preventDefault();
  });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...