Stop jquery-ui datepicker from showing in beforeShow

混江龙づ霸主 提交于 2019-12-19 09:19:19

问题


I wish to do something like this:

var showme = false;

$('#mydatepicker').datepicker({
    beforeShow: function(input, inst) {
        if(!showme) {
            //stop showing
        }
    }
});

But disable, nor hide seems to work. I'd like to do something that works like preventDefault, but I don't think it'll work in this case.


回答1:


Looking through the source, it doesn't look like that is possible currently. You might be able to request that as a feature. If you really want to make it stop (though this is probably a bad idea) you could throw an error in that function. Though this will cause errors to show up for your page and probably is not an option.


Note: I submitted a patch for this and it's now in the released jQuery UI (at least v1.9 and later), although the documentation doesn't mention it. return false in beforeShow will prevent the datepicker from appearing (now).




回答2:


I had the same question, and have discovered that you can now use return false; to prevent the calendar from showing.

$('#mydatepicker').datepicker({
    beforeShow: function(input, inst) {
        if(!showme) {
            return false;
        }
    }
});

I'm using version 1.7.2 of jQuery UI




回答3:


It might be better to simply control the showing of the datepicker with the disable and enable method parameters.

$('#mydatepicker').datepicker('disable'); //Don't show datepicker 

$('#mydatepicker').datepicker('enable'); //Show datepicker

It would seem to accomplish the same purpose that you are seeking.




回答4:


The best way to prevent datepicker from opening is to use the following function in beforeShow:

$("#datepicker").datepicker("close");

this forces datepicker to close before it even get's shown.




回答5:


Seems to be the closest I could get too, but I don't really want to have to use setTimeout here, it seems a bit hacky:

        var showme = false;

        $('#mydatepicker').datepicker({
            beforeShow: function (input, inst) {

                if (!showme) {

                    setTimeout(function () { $(input).datepicker("hide"); }, 1);

                }
            }
        });



回答6:


Since this patch has not been merged in, I was able to come up with a hack that works pretty well. Let me know what you think:

beforeShow: function(){
    var start = $('#dtFrom').val()
    if($.trim(start)!=''){
        start = $('#dtFrom').datepicker('getDate');
        $('#DateToDisplay').datepicker('option', 'minDate', new Date(start.getTime()));
    }
},
    onClose: function() {
    var start = $('#dtFrom').val()
    if($.trim(start)==''){
        alert('Error! Please select a start date\/time prior to setting the date to display.');
        $('#DateToDisplay').val('')
    }
}


来源:https://stackoverflow.com/questions/6599009/stop-jquery-ui-datepicker-from-showing-in-beforeshow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!