我有一个datepicker
函数似乎正在运行,但onSelect
函数无效,我看不到alert()
$(function() {
var divContent = '<input type="text" placeholder="Enter date" class="dupInput" id="gl_img_datepicker" readonly=""><button id="gl_img_datepicker_init">Date</button>';
jQuery('body').on('click', '#gl_img_datepicker_init', function(e) {
jQuery("#gl_img_datepicker").datepicker('show');
});
//onselect datepicker function
jQuery('#gl_img_datepicker').datepicker('option', 'onSelect',
function(dateText, inst) {
alert('glImgExpiry-->' + dateText);
});
jQuery('body').on('click', '#mybutton', function(e) {
jQuery("#my_div").append(divContent)
jQuery("#gl_img_datepicker").datepicker();
});
});
我已经看过以下问题,但没有帮助。
Datepicker onSelect Not Firing
jquery datepicker onselect not working
Datepicker onSelect not working
jQuery Datepicker onSelect is not working
谢谢
答案 0 :(得分:2)
您尝试将该事件分配给尚未存在的字段(意味着在创建#gl_img_datepicker并将其插入文档之前)。试试这样(这里是JSFiddle):
$(function() {
var divContent = '<input type="text" placeholder="Enter date" class="dupInput" id="gl_img_datepicker" readonly=""><button id="gl_img_datepicker_init">Date</button>';
jQuery('body').on('click', '#gl_img_datepicker_init', function(e) {
jQuery("#gl_img_datepicker").datepicker('show');
});
jQuery('body').on('click', '#mybutton', function(e) {
jQuery("#my_div").append(divContent)
jQuery("#gl_img_datepicker").datepicker();
// We have already appended the divContent to the document; we
// can continue with the actual datepicker event for that field
//onselect datepicker function
jQuery('#gl_img_datepicker').datepicker('option', 'onSelect',
function(dateText, inst) {
alert('glImgExpiry-->' + dateText);
});
});
});