如何在 flatpickr 中设置条件配置选项

How to set conditional config options in flatpickr

我成功地使用 flatpickr 作为我最喜欢的日期选择器,但现在我必须将“defaultDate”输入设为条件。

if(defDateExists){
    defaultDate: ucheckout,
}

这破坏了我的功能。我不知道如何有条件地使用选项。请帮忙。谢谢!

function _flattie() {
    const fpx = flatpickr(".flatpickr", {
        minDate: new Date().fp_incr(1), // 1 days from now
        dateFormat: "Y-m-d",
        disableMobile: "true",
        if(defDateExists){
            defaultDate: checkout,
        }
        monthSelectorType: "static",
        inline: true,
        onChange: function() {
            selectedDate = new Date(tempDate).toString("yyyy-MM-dd");
            stringDate = new Date(tempDate).toString("MMM dS yyyy"); 
            }
        }
    });
}

试试 ternary conditionals:

function _flattie() {
const fpx = flatpickr(".flatpickr", {
    minDate: new Date().fp_incr(1), // 1 days from now
    dateFormat: "Y-m-d",
    disableMobile: "true",
    defaultDate: defDateExists ? checkout : null,
    monthSelectorType: "static",
    inline: true,
    onChange: function() {
        selectedDate = new Date(tempDate).toString("yyyy-MM-dd");
        stringDate = new Date(tempDate).toString("MMM dS yyyy"); 
        }
    }
});}