2012-09-24 44 views
-1

我有一个jQuery datepicker,除了在updateTable中的Ajax完成时工作。 日历单元格中的数字消失,并且防止了单元格的选择。无论updateTable的结果如何,都会发生这种情况。问题是什么?Datepicker Ajax中断

<head> 

<script type="text/javascript"> 

    $(document).ready(function() { 

     initDatepicker(); 

     $("#btn").live('click', function (event) { 

       $.ajax({ 
       url: '@Url.Action("Add")', 
       type: 'POST', 
       contentType: 'application/json', 
       success: function (result) { 
         updateTable(); 
       } 
      }); 
     }); 

    }); 

    function initDatepicker() {    
     $('.datepicker').datepicker(); 
    }; 


    function updateTable() {   
     $.ajax({ 
      url: '@Url.Action("UpdateTable")', 
      type: 'POST', 
      contentType: 'application/json', 
      success: function (result) { 
       $("#table").append(result); 
      }, 
     }); 
    }; 

</script> 

</head> 

<body> 
     <div> 
     @RenderBody() 
     </div> 

     <div id="table"> 
     </div> 
</body> 

回答

0

您必须在更新Ajax之后重新初始化日期选择器,因为您更改了DOM结构,而前一个不再有效。因此,在更新表后,再次调用initDatepicker();

function updateTable() {   
     $.ajax({ 
      url: '@Url.Action("UpdateTable")', 
      type: 'POST', 
      contentType: 'application/json', 
      success: function (result) { 
       $("#table").append(result); 
       // here call again the init 
       initDatepicker();  
      }, 
     }); 
    }; 
+0

它没有效果。 – Mixel

+0

@Mixel对你的代码有点工作(如果你明白了),不要等我解决它,或者给我一个URL来检查它。答案“它没有任何作用”,除了你根本不知道什么之外,没有任何线索。 – Aristos