2013-10-03 62 views
0

我有一個jquery對話框,其中我加載了兩個partialview,一個簡單的master/detail 當我回到master後保存細節,單擊按鈕不響應。jquery對話框第二次加載mvc partialview

我張貼一些代碼:

MASTER

 $('#dialogActivities').dialog({ 
     autoOpen: false, 
     open: function (event, ui) { 
      var id = $(this).data('id'); 
      var urlList = '@Url.Action("ActivityList", "Contacts")'; 

      $(this).load(urlList, function() 
      { 
       $(".k-grid-ActivityEdit").bind("click", function() 
       { 
        var grid = $("#gridYear").data("kendoGrid"); 
        var selectedData = grid.dataItem(grid.select()); 
        if (selectedData) 
        { 
         var urlEdit = '@Url.Action("ActivityEdit", "Contacts")'; 
         $('#dialogActivities').dialog().load(urlEdit); 
        } 
       }); 
      }); 
     } 
    }); 

DETAIL

$(document).on('submit', '#frmActivityEdit', function() 
{ 
    $.ajax({ 
     cache: false, 
     url: this.action, 
     type: this.method, 
     data: $(this).serialize(), 
     success: function (result) 
     { 
      if (result.success) 
      { 
       var model = @Html.Raw(Json.Encode(Model)); 
       var urlList = '@Url.Action("ActivityList", "Contacts")'; 

       $('#dialogActivities').dialog().load(urlList); 
      } 
     } 
    }); 
    return false; 
}); 

回答

0

請嘗試更換這段代碼:

$(".k-grid-ActivityEdit").bind("click", function() 
      { 
       var grid = $("#gridYear").data("kendoGrid"); 
       var selectedData = grid.dataItem(grid.select()); 
       if (selectedData) 
       { 
        var urlEdit = '@Url.Action("ActivityEdit", "Contacts")'; 
        $('#dialogActivities').dialog().load(urlEdit); 
       } 
      }); 

有了這一個:

$(document).on("click", ".k-grid-ActivityEdit", function() 
      { 
       var grid = $("#gridYear").data("kendoGrid"); 
       var selectedData = grid.dataItem(grid.select()); 
       if (selectedData) 
       { 
        var urlEdit = '@Url.Action("ActivityEdit", "Contacts")'; 
        $('#dialogActivities').dialog().load(urlEdit); 
       } 
      }); 

區別在於使用on函數進行綁定也適用於動態添加的元素。它可以解決你的問題。

+0

它的工作!謝謝!你拯救我的一天! – Berry

+0

這很好:)不客氣 –

+0

現在我有另一個問題,第一次我點擊,打電話正確,但第二次,它打電話兩次,第三次,這就像它需要註冊事件...我嘗試與:$(「。k-grid-ActivityDelete」)。unbind('click');但它不起作用 – Berry