2009-05-04 18 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'); 
    } 
} 

それは働いていません。これを達成する別の方法がありますか?迅速な対応の人たちのための


感謝。私はあなたの提案を試みたが、パラメータとして渡される関数を実行していない。 custom_confirm機能をクリーンアップ

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

、近いオプションを追加しました:

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

それを見つけました。あなたが別の関数にパラメータとしての機能を渡している場合は、

$("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

を助け変数

action 

希望funciton

action() 

として代わりのように、パラメータを呼び出す必要があります助けてくれてありがとう。私はjQueryのUIダイアログプラグインを使用することを望んでいた....しかし、それが複雑すぎる場合は、ネイティブのjavascriptの確認機能を使用することができます。 – timborden

+0

これは通常のjQueryUIダイアログです。 2回目に進むことを拒否した後、ダイアログが戻ってくるようにする必要があります。 – cgp

0

はい、または単に:

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

ええ、私たちはどちらも正しいですが、私はかなりアイロンをかけていないいくつかの他の問題がコードにあります。 – cgp

関連する問題