为什么在添加字段时以$ .ajax()成功初始化jQuery?

时间:2016-06-30 08:01:31

标签: javascript jquery ajax jquery-ui datepicker

我在.datepickerTime.datePickerDate类上使用jQuery日期选择器。

$('.datepickerTime').datetimepicker({
    'timepicker': false,
    'mask': false,
    'format': 'M d,Y',
    'minDate': 0,
    'scrollMonth': false,
    'scrollInput': false,
    'allowTimes': [ '00:00','00:30','01:00','01:30','02:00','02:30','03:00','03:30','04:00','04:30','05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30' ]
});

当页面加载时,所有日期选择器都在类的基础上工作。但是当我在具有datepicker类的表单中添加新字段时,日期选择器不起作用。

我已经得到了解决方案,我必须使用ajax成功再次初始化jquery。

我很困惑为什么我们需要在AJAX成功时初始化datepicker,因为我们已经在页面上有了jQuery?

我是jquery的新手,想要纠正我的基本概念:-),在此先感谢。

2 个答案:

答案 0 :(得分:0)

您需要re-initialize datepicker ajax-success element,因为DOM会在您初始化plugin后添加到ajax-success。您将在这里有2个选项。

  • document内重新初始化 或
  • bodyelement或任何datepicker上对其进行初始化,该DOM将是您$('body').on('focus',".datepickerTime", function(){ $(this).datetimepicker({ 'timepicker': false, 'mask': false, 'format': 'M d,Y', 'minDate': 0, 'scrollMonth': false, 'scrollInput': false, 'allowTimes': [ '00:00','00:30','01:00','01:30','02:00','02:30','03:00','03:30','04:00','04:30','05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30' ] }); }); 元素的父级,并且会在初始//CatchStatement[//FormalParameter//VariableDeclaratorId/@Image = //ThrowStatement//Arguments//Expression//Name/@Image] 加载时生成。

对于你的第二种方法,下面是

<tr>
<span class="originalAirportId">123</span>
<span class="airportName">123</span>
<span class="cityName">123</span>
<span class="provinceName">123</span>
<span class="provinceId">123</span>
<span class="countryId">123</span>
<span class="countryName">123</span>
</tr>
<tr>
<span class="originalAirportId">123</span>
<span class="airportName">123</span>
<span class="cityName">123</span>
<span class="provinceName">123</span>
<span class="provinceId">123</span>
<span class="countryId">123</span>
<span class="countryName">123</span>
</tr>
.....
.....
.....

答案 1 :(得分:0)

你应该为新元素添加新的类或id,并为你的新元素调用datetimepicker到init