我通过简单地将 HTML 字段附加到 div 来动态生成日期选择器:
<input type="text" value="" readonly="readonly" name="tier[2][publication_date]" id="publication_date_2" size="10" maxlength="10" tabindex="6" class="publication_date hasDatepicker">
<input type="hidden" name="tier[2][publication_date_db]" id="publication_date_db_2" value="">
由于我们存储日期的方式,我们为日期选择器提供了一个单独的字段(altfield),用于存储用户选择的数据库格式化日期。
为了处理多个日期选择器的选择,我分配一个类并使用 livequery 以便在页面加载后检测 onClicks:
$(".publication_date").livequery(function() {
$(this).datepicker({
dateFormat: "dd M yy",
changeYear: true,
changeMonth: true,
onSelect: function(dateText, inst) {
console.log(inst);
}
});
});
我有一个问题,日期选择器如何知道要填充哪个替代字段?在单个日期选择器上,通常包括:
altField: '#start_date_datepicker_altfield',
altFormat: 'yy-mm-dd',
人口发生。
最佳答案
我知道文档说它需要一个选择器,但它也可以接受一个 jQuery 对象,所以只需使用 $(this).next()
获取隐藏字段,如下所示:
$(".publication_date").livequery(function() {
$(this).datepicker({
altField: $(this).next(),
altFormat: 'yy-mm-dd',
dateFormat: "dd M yy",
changeYear: true,
changeMonth: true,
onSelect: function(dateText, inst) {
console.log(inst);
}
});
});
由于大多数插件都归结为 $(input)
,输入可以是选择器或 jQuery 对象或 DOM 元素,它仍然可以正常工作很好:)
关于jquery - 使用 altFields 动态生成 jQuery Datepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3657527/