2012-02-22 63 views
0

我已經嘗試了最近3周獲得ajaxLink以使用jQuery對話框。我有一個刪除書籤功能,並希望出現一個對話框,其中您必須確認您要刪除該書籤,才能觸發Ajax請求並刪除該書籤。Zend Framework Ajax使用jQuery對話框工作的鏈接

我認爲我必須添加一些東西給beforeSend函數,但我無法弄清楚需要寫入什麼。有人可以建議我需要做什麼嗎?我希望有人知道答案,我用完了想法。非常感謝您提前!

這裏我的源代碼至今:

echo $this->ajaxLink("Remove Bookmark","/bookmark/remove/article ".$this->escape($entry->id), 
       array(
        'id' => '', 
        'class' => 'btn orange delete dialog-confirm', 
        'dataType'=>'JSON', 
        'method' => 'post', 
        'update' => '.bookmark', 
        'beforeSend' => '????', 
        'complete' => '$("."+data+"").remove();if ($(".watchlist").length == 0){$(".watch").append("<p>No items watched</p>")}' 
      )); 

我的jQuery的對話框:

$('.dialog-confirm').click(function(e){ 

    e.preventDefault(); 
    var URL = $(this).attr("href"); 
    $(this).css('display','block'); 

    $('#dialogbox').dialog({ 

     resizable: false, 
     height:180, 
     width:350, 
     modal: true, 
     closeOnEscape: true, 
     buttons: { 
      "Yes, delete bookmark": function() { 
       window.location.href = URL; 
       return true; 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     } 

     }); 
}); 

回答

1

你的Ajax請求必須被觸發只有在用戶單擊「是,刪除書籤」按鈕。因此,您需要爲此按鈕添加一個jQuery ajax函數並刪除您的第一個ajaxLink()。現在

//in your view.phtml 
<div id="dialogbox" style="display:none">Do you really want to remove this bookmark?</div> 
<span style="cursor:pointer" class="btn orange delete dialog-confirm" id="1">Remove Bookmark</span> 

<?php $this->jQuery()->addOnload(' 
$(function() { 
    var id; 

    $("#dialogbox").dialog({ 
     resizable: false, 
     height:180, 
     width:350, 
     modal: true, 
     buttons: { 
      "Yes, delete bookmark": function() { 
       $.ajax({ 
        type: "POST", 
        url: "/bookmark/remove/article", 
        dataType: "json", 
        data: "id="+id, 
        success: function(data, textStatus, jqXHR) { 
         $("."+data+"").remove(); 
         if ($(".watchlist").length == 0) { 
          $(".watch").append("<p>No items watched</p>") 
         } 
        } 
       }); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     }, 
    }); 


    $(".delete").click(function(e) { 
     id = this.id; 
     console.log(id); 
     $("#dialogbox").dialog("open"); 
    }); 
}); 
'); ?> 

,如果你點擊刪除書籤,它會顯示一個jQuery對話框有兩個按鈕:取消「是的,刪除書籤」,一旦你點擊是的,它會調用jQuery.ajax函數並將id作爲參數發送到/ bookmark/remove/article。

+0

非常好,它的作品!我無法相信我已經嘗試了很多年,現在它很簡單。非常感謝。唯一的想法是在「是」功能後面「關閉」,否則對話框將保持打開狀態,即使書籤在後臺被刪除。非常感謝你,不要以爲我會以其他方式發現這一點。現在,我將添加一些消息來顯示,該書籤被刪除,並找出爲什麼對話框已打開,只要我進入該網站。再次感謝你 ! – Luka 2012-02-22 16:22:09

+0

非常歡迎。 – Liyali 2012-02-22 18:11:19