简体   繁体   中英

jQuery DatePicker disappears when i click Next/Previous buttons

My project has a datepicker which has buttons to go previous/next months. it looks like this: 在此输入图像描述

but when i click the buttons on right and left corner of the control, it suddenly disappears.

jquery function:

 $( "#departDate" ).datepicker({
        showOtherMonths: true,
        //inline:true,
        dateFormat: 'dd-mm-yy',
        numberOfMonths: 2,
        selectOtherMonths: true,
        minDate: 0,
        onClose: function (selectedDate) {
            $("#arriveDate").datepicker("option", "minDate", selectedDate);
            if (!single && $("#arriveDate").val()=="")
                $("#arriveDate").focus();
        }
    });

i tried to comment out onClose: and minDate but none of these fixed problem.

When i remove blur function on this datepicker, it stops disappearing. But i need to make it disappear when click anywhere out of this tool.

you should add a click event an check for all elements in your datepicker. this might be useful for you:

$(document).on("click", function (e) {
        var elem = $(e.target);
        if (!elem.hasClass("hasDatepicker") &&
            !elem.hasClass("ui-datepicker") &&
            !elem.hasClass("ui-icon") &&
            !elem.hasClass("ui-datepicker-next") &&
            !elem.hasClass("ui-datepicker-prev") &&
            !$(elem).parents(".ui-datepicker").length) {
            $('#departDate').datepicker('hide');
        }

我认为你正在使用两个js文件,你的下一个按钮将覆盖,所以尝试在你的html页面上更改js文件的序列

I don't find this problem persist in my fiddle .

Since you mentioned,

When i remove blur function on this datepicker, it stops disappearing. But i need to make it disappear when click anywhere out of this tool?

I have a workaround solution like whenever you click outside the datepicker, it will be hidden.

$(document).on('click',function (e) {
    var container = $("#departDate");    
    if (!container.is(e.target) 
        && container.has(e.target).length === 0) {
        container.hide();
    }
});

Check this working in JSFiddle

Hope you can understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM