我正在使用一个现有的JavaScript库,它执行Ajax调用,然后执行一个可选的回调函数。回调函数将传递正在加载到DOM中的HTML字符串。我需要将jquery日期选择器分配给该HTML字符串中属于日历类的所有元素。
Ajax查询中HTML字符串的一个简化示例是:
<tr id='1234'>
<td>
<input type="text" value="somevalue" />
<input type="text" class="calendar" value="21/12/2010" />
</td>
<td>
<input type="text" value="someothervalue" />
<input type="text" class="calendar" value="21/12/2011" />
</td>
</tr>
我可以在回调函数中执行以下操作,将datepicker设置为两个必需的输入,但这会导致datepicker发生一些奇怪的事情,例如从2010年跳到1900年的下一个按钮和跳到1899年的上一个按钮。
$(".calendar").datepicker('destroy').datepicker({ dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', duration: 200, gotoCurrent: true, changeYear: true, yearRange: 'c-120:c' });
因此,我只想将语句限制在提供给回调函数的HTML片段中的元素。我可以使用什么jquery模式来实现以下结果,注意html参数是一个字符串:
function SetCalendar(html) {
$("html.children .calendar").datepicker('destroy').datepicker({ dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', duration: 200, gotoCurrent: true, changeYear: true, yearRange: 'c-120:c' });
}