$(document).on(’click’,”,function(){})无法正常工作

hae,我正在使用$(document).on('click','',function(){}); 但它不能正常工作我的代码是:

   $(function () { $(document).on('click', '.date-picker', function () { $(this).datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'MM yy', onClose: function (dateText, inst) { var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); $(this).datepicker('setDate', new Date(year, month, 1)); } }); }); });  table_html = ''; $('#variables').append(table_html); 

上面的代码创建了一个月份选择器。 它工作正常。
但是当我第一次点击时,月份选择器控件什么都不做。 当我在它外面点击并再次点击它时,它有效。
关于如何在第一次点击时检测到点击事件的任何想法或建议将受到高度赞赏

试试show()

 $(function () { $(document).on('click', '.date-picker', function () { var datepicker = $(this).data('uidatepicker'); if(datepicker) return; $(this).datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'MM yy', onClose: function (dateText, inst) { var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); $(this).datepicker('setDate', new Date(year, month, 1)); } }).datepicker('show'); }); }); 

演示: 小提琴