附加的jQuery datepicker无法正常工作

Jaz*_*Cat 0 jquery datepicker

当我试图显示多个字段时,我遇到了这个问题,并且应该在那些datepicker上运行onclick,如果我将代码放在javascript之外,它就可以正常工作,因此我$ .each循环一定有问题。它或与附加,但不知如何即时通讯无法正常工作。

$(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>");
                            });


                        });


                }
            });
Run Code Online (Sandbox Code Playgroud)

帖子->在控制器中运行插入查询,并返回循环出的一些json数据。对于返回的每一行,我想要一个inputfield,一个可工作的:)非常感谢您的帮助,这种方法已经为时已久了:/

小智 5

对jQuery 1.9+使用.on()函数

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