开发者

Stop jquery-ui datepicker from showing in beforeShow

开发者 https://www.devze.com 2023-03-18 16:29 出处:网络
I wish to do something like this: var showme = false; $(\'#mydatepicker\').datepicker({ beforeShow: function(input, inst) {

I wish to do something like this:

var showme = false;

$('#mydatepicker').datepicker({
    beforeShow: function(input, inst) {
        if(!sh开发者_如何学Cowme) {
            //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.


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


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).


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.


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.


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);

                }
            }
        });


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('')
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号