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

时间:2013-10-16 09:08:44

标签: javascript jquery

hae,正在与$(document).on('click','',function(){});合作,但它不能按我的意愿工作 下面是我的代码:

 <script type="text/javascript">
        $(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));
                    }
                });
            });
        });
</script>



table_html = '<td style="min-width: 200px;"><input name="' + control_id + '" id="' + control_id + '" value="' + row_val + '" type="date" data-role="datebox" data-options=\'{"mode": "calbox", "useClearButton": true}\' ></td>';
  $('#variables').append(table_html);

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

1 个答案:

答案 0 :(得分:1)

尝试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');
    });
});

演示:Fiddle

相关问题