2009-05-04 153 views
2

我試圖使用jQuery UI對話框來顯示執行動作之前的確認...在這種情況下,導航到選定的鏈接....但在另一種情況下,我可能想使用AJAX刪除。jQuery UI對話框確認

我想我可以通過這一行動的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();}}); 
} 

回答

2

想通了。如果你傳遞一個函數作爲參數傳遞給另一個函數,你需要調用的參數作爲一個功能可按

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

您需要在封閉的this變量傳遞。

See this example.(改進)

$("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(); 
    }); 
}); 
+0

再次感謝您的幫助。我希望使用jQuery UI對話插件....但如果它變得太複雜,我可以使用本地JavaScript確認功能。 – timborden

+0

現在它是常規的jQueryUI對話框。在他們要求不再進行第二次操作後,您需要讓對話回來。 – cgp

0

是的,或者乾脆:

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

是的,我們都是對的,但是在代碼中還有一些其他問題我還沒有解決。 – cgp