2013-12-08 108 views
4

我有以下代碼JQuery的日期選擇器

<html> 
    <head> 
     //included all jquery related stuff ..not shown here 
    </head> 
    <body> 
     <button id = 'btn' /> 
     <div id = 'ct'> 
      <?php echo file_get_contents('my_ajax_stuff.php'); ?> 
     </div> 
    </body> 
    <script> 
     $('.datepicker').datepicker({dateFormat: "dd-mm-yy"}); 
     $('#btn').click(function() { 
      $.ajax({ 
      type: "GET", 
      url: "my_ajax_stuff.php" , 
      success: function(response) { 
       $('#ct').html(response); 
       /*added following line to solve this issue ..but not worked*/ 
       //$(".datepicker").datepicker({dateFormat: "dd-mm-yy"}); 

      } , 
      error: function() { 
       $('#ct').html("Some problem fetching data.Please try again"); 
      } 
     }); 
     }); 
    </script> 
</html> 

my_ajax_stuff.php

包含一流的jQuery UI的日期選擇器= '日期選擇器' 。當第一次加載datepicker的作品。但當我點擊按鈕重新加載它,內容被替換爲新內容。但datepicker不工作。正如你所見,將日期選擇器放在ajax成功處理程序中。但它也失敗了。問題是什麼。它如何解決?

+0

我已經試過了,這是在阿賈克斯成功處理程序評論here..But沒有區別 –

+0

什麼給你的成功回調:'$(「.datepicker」)。長度?控制檯中有任何錯誤?順便說一句,你可以通過在mouseenter上需要(尚未完成)重新初始化datepicker來使用解決方法。 Mouseenter應該被授權 –

+1

.length返回1.請在控制檯 –

回答

13

您需要reinitialize在阿賈克斯成功的日期選擇器

$('.datepicker').datepicker({dateFormat: "dd-mm-yy"}); 

$('#btn').click(function() { 
    $.ajax({ 
     type: "GET", 
     url: "my_ajax_stuff.php" , 
     success: function(response) { 

      $('#ct').html(response); 
      $("#datepicker").datepicker(); 
      /*added following line to solve this issue ..but not worked*/ 
      //$(".datepicker").datepicker({dateFormat: "dd-mm-yy"}); 

     } , 
     error: function() { 
      $('#ct').html("Some problem fetching data.Please try again"); 
     } 
    }); 
}); 
+0

我已經試過用 $(「.datepicker」).datepicker ({dateFormat:「dd-mm-yy」}); 但沒有工作 –

+0

@JinuJosephDaniel在這裏工作得很好.... http://jsfiddle.net/9rVK4/其他東西在你的代碼中是錯誤的 – charlietfl

+1

喲已經嘗試過之前阿賈克斯把它放在你的成功 –

3

你正在尋找的答案可能類似於這樣的問題: jQuery datepicker won't work on a AJAX added html element

你更換日期選擇器元素在DOM與阿賈克斯響應。 這是第一次這樣工作的原因是PHP在第一次加載時已經在HTML中呈現my_ajax_stuff.php,所以它在DOM中可用於jQuery。

// do this once the DOM's available... 
$(function(){ 

// this line will add an event handler to the selected inputs, both 
// current and future, whenever they are clicked... 
// this is delegation at work, and you can use any containing element 
// you like - I just used the "body" tag for convenience... 
    $("body").on("click", ".datepicker", function(){ 
      $(this).datepicker(); 
      $(this).datepicker("show"); 
    }); 
}); 
+2

我會過濾它以防萬一:'$(「body」)。on(「click」,「.datepicker:not(.hasDatepicker)」,功能(){...});' –

+0

這個解決方案不工作:( –

+0

@JinuJosephDaniel你可以提供.datepicker元素的HTML標記,因爲它是在迴應和我問你10分鐘前??? –

0

我遇到了這個問題,因爲我遇到了和OP一樣的問題。

Mooed Farooqui在阿賈克斯成功的reinitialize的建議工作,但不是最初。

最後我還打電話給datepicker函數,我的等效函數是my_ajax_stuff.php。我把它拿出來後,它完美的按鈕重新加載和回發。希望這可以幫助那些絆倒這個老問題的人...

0

.ajaxComplete函數內調用你的選擇器。

$(document).ready(function() { 
    // setup picker here... 
}); 

$(document).ajaxComplete(function() { 
    // setup picker here... 
}); 

這裏是一個link