我正在尝试在动态创建的元素上调用datepicker,但不起作用。我知道我的格式不正确。有人知道吗?

    $("body").on('click', '.startNew', function() {
        $(".startNew").datepicker({
           dateFormat : "yy-mm-dd 00:00:00",
           numberOfMonths: 2,
           onSelect: function(selected) {
             $(".endNew").datepicker("option","minDate", selected)
           }
        });
    });
    $("body").on('click', '.endNew', function() {
       $(".endNew").datepicker({
           dateFormat : "yy-mm-dd 00:00:00",
           numberOfMonths: 2,
           onSelect: function(selected) {
              $(".startNew").datepicker("option","maxDate", selected)
           }
       });
    });

最佳答案

更新。尝试更改(正文->文档;单击->焦点)



$(body).on('click',".startNew", function(){
    $(".startNew").datepicker({

$(body).on('click',".endNew", function(){
    $(".endNew").datepicker({




$(document).on('focus',".startNew", function(){
    $(this).datepicker({

$(document).on('focus',".endNew", function(){
    $(this).datepicker({


工作FIDDLE

的HTML

Start: <input class='endNew'><br/><br/>
End: <input class='startNew'>


JS

$(document).on('focus',".startNew", function(){
  $(this).datepicker({
    dateFormat : "yy-mm-dd 00:00:00",
    numberOfMonths: 2,
    onSelect: function(selected) {
      $(".endNew").datepicker("option","minDate", selected)
    }
  });
});


$(document).on('focus',".endNew", function(){
  $(this).datepicker({
    dateFormat : "yy-mm-dd 00:00:00",
    numberOfMonths: 2,
    onSelect: function(selected) {
      $(".startNew").datepicker("option","maxDate", selected)
    }
   });
});

09-07 04:47