2011-10-24 187 views
0

我有這個問題,當我試圖顯示一些字段,並onclick這些日期選擇器應該運行,如果我把代碼以外的JavaScript它工作得很好,所以一定有東西錯誤,我怎麼循環它或與追加,但不知何故我沒有得到它的工作。jQuery datepicker追加不工作

$(function() { 

     $(".datepicker").datepicker({ 
      changeMonth: true, 
      changeYear: true, 
      dateFormat: "yy-mm-dd" 
     }); 

var val = []; 

       $(':checkbox:checked').each(function(i){ 

       val[i] = $(this).attr("id"); 

       }); 


       if(val == "") 
       { 

       alert('no selection'); 

       } 
       else 
       { 

        $.post("add_stuff", { "post" : val, "csrf" : cct }, 
         function(data) 
         { 
          $("#content").empty(); 

          json = eval('('+data+')'); 

          $.each(json.datecheck, function() { 
           $("#content").append("<h2>" + this.pName + " - " + this.pDesc + "</h2>"); 
           $("#content").append("<p>Datum: <input type='text' class='datepicker'></p>"); 
          }); 


         }); 


       } 
      }); 

的崗位 - >運行在控制器插入查詢,並返回一些JSON數據多數民衆贊成環路輸出。 以及對這些返回我希望有一個inputfield,一個正在運行的:) 會很感激一些幫助,一直用這種方式太長時間苦苦掙扎的每一行:/

回答

3

使用。對()的jQuery 1.9+

$(document).on('click', '.datepicker', function(){ 
    $(this).timePicker({ 
     changeMonth: true, 
     changeYear: true, 
     dateFormat: "yy-mm-dd" 
    }).focus(); 
    $(this).removeClass('datepicker'); 
}); 
1

目前的約束力不作爲的單元工作不存在。
作爲添加的輸入字段是動態的,你需要使用JQuery live

示例 -

$('.datepicker').live('click', function(){ 
    $(this).timePicker({ 
     changeMonth: true, 
     changeYear: true, 
     dateFormat: "yy-mm-dd" 
    }).focus(); 
    $(this).removeClass('datepicker'); 
}); 
+0

還以爲是功能是類似的東西,非常感謝,非常完美:) – JazzCat

+0

如何使用這個,當我們把'從日期''datepicker'? – Kirataka

+0

不幸的是,'live'在1.9中被刪除了,你可以修改以適應現代化的解決方案嗎? – NoReceipt4Panda