更改 DatePicker 更改下拉列表中选择的选项

Change DatePicker changing the option selected in a dropdown

我想根据下拉菜单中 select 的选项(每日或每月 selection)更改日期选择器的格式。这是我的代码:

$("#date-format").change(function () {
    if ($(this).find("option:selected").text() == "Daily") {
        $('.date-picker').datepicker({
            dateFormat: "dd/mm/yy"
        });
    }
    else if ((this).find("option:selected").text() == "Monthly") {
        $('.date-picker').datepicker({
            dateFormat: "mm/yy",
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            onClose: function (dateText, inst) {

                function isDonePressed() {
                    return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
                }

                if (isDonePressed()) {
                    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)).trigger('change');

                    $('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
                }
            },
            beforeShow: function (input, inst) {

                inst.dpDiv.addClass('month_year_datepicker')

                if ((datestr = $(this).val()).length > 0) {
                    year = datestr.substring(datestr.length - 4, datestr.length);
                    month = datestr.substring(0, 2);
                    $(this).datepicker('option', 'defaultDate', new Date(year, month - 1, 1));
                    $(this).datepicker('setDate', new Date(year, month - 1, 1));
                    $(".ui-datepicker-calendar").hide();
                }
            }
        })
    }
})

你知道第一次实现日期选择器后如何更改格式吗?

您可以使用 option 方法,在您的情况下是这样的:

$('.date-picker').datepicker( "option", "dateFormat", "mm/yy");