首页 > 解决方案 > 调用pickdate时如何添加条件?

问题描述

我的html是这样的:

<input id="appdate" type="text" class="required" placeholder="Select Date">

我的 javascript 是这样的:

...
$(document).ready(function () {
    ...
    $('#appdate').change(function () {
        console.log('when the retrieveSchedule function is called, this is not executed')
        var date = $('#appdate').val();
        ...
    });
});


appointment = function () {
    function retrieveSchedule({ id, url }) {
        ...
        $.ajax({
            url: url,
            type: 'GET',
            data: '',
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                ...
                var $input = $('#appdate').pickadate({
                    disable: _disableDate,
                    formatSubmit: 'yyyy-mm-dd',
                    min: someDate,
                    max: new Date((someDate.getFullYear() + 1), (someDate.getMonth() - 1), someDate.getDate()),
                    selectMonths: true, 
                    selectYears: true, 
                    close: 'Ok',
                    autoClose: false,
                    closeOnSelect: false, // Close upon selecting a date,
                    onClose: function () { $('.picker__input').removeClass('picker__input--target'); $('.picker').blur(); },
                    onRender: function () {
                        $('#appdate_root .picker__day-display').append('<div class="cal-notif"></div>');
                        var loadingcal = '<div class="cal-loading preloader-background">' +
                            '<div class="preloader-wrapper big active center">' +
                            '<div class="spinner-layer spinner-blue-only">' +
                            '<div class="circle-clipper left">' +
                            '<div class="circle"></div>' +
                            '</div>' +
                            '<div class="gap-patch">' +
                            '<div class="circle"></div>' +
                            '</div>' +
                            '<div class="circle-clipper right">' +
                            '<div class="circle"></div>' +
                            '</div>' +
                            '</div>' +
                            '</div>' +
                            '</div>';
                        $('#appdate_root .picker__calendar-container').append(loadingcal);
                    }
                });

                // Use the picker object directly.
                var picker = $input.pickadate('picker');
                picker.clear();
                if (picker.get('start')) {
                    var getdisable = picker.get('disable');
                    picker.set('enable', [1, 2, 3, 4, 5, 6, 7]);
                    picker.set('disable', _disableDate);
                    getdisable = picker.get('disable');
                    picker.render();

                } else { console.log('false picker'); }
            },
            error: function (result) {
                ...
            }
        }).done(function () {
            ...
        });
    }
    return { retrieveSchedule: retrieveSchedule }
}();

我没有显示我所有的脚本。我只显示我遇到的问题的详细信息

上面的js文件运行时,首先会调用retrieveSchedule函数。如果 ajax 成功,它将创建一个 pickdate 并将其显示在 id = appdate 的输入文本中

我的问题是当pickdate成功显示在id = appdate的输入文本中时,它也会在$('#appdate').change(function({. 我希望里面的语句$('#appdate').change(function({不运行。所以它只在选择pickdate时运行。不显示pickdate

我如何制定条件?

标签: javascriptjqueryhtmldatedatepicker

解决方案


如何创建这样的检查变量

var appdateCreated = false;

$('#appdate').change(function () {
  if(appdateCreated) {
    console.log('when the retrieveSchedule function is called, this is not executed')
  } else {
    appdateCreated = true
  }
});

推荐阅读