我的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时运行。不显示选择日期

我如何创造条件?

最佳答案

如何创建像这样的检查变量

var appdateCreated = false;

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

关于javascript - 调用pickdate时如何添加条件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58046894/

10-11 13:03