jquery datepicker change position dynamically

会有一股神秘感。 提交于 2019-12-14 02:35:53

问题


I use several datepickers on my page and in some cases i hide a div with one of them. After that the position of the other datepickers is not correct anymore, they are way above the input. I think that is because the place is determined on initialisation, because if i show the div again the position is correct.

But how to make the position that it changes if an other datepicker (div) hides?


回答1:


Perhaps set the position in the beforeShow event:

$(".is-datepicker").datepicker("option", "beforeShow", function(input, inst){
   $(inst.dpDiv).position({
      my: "left top",
      at: "left bottom",
      of: $(input)
 });

position() was added in version 1.8 http://api.jqueryui.com/position/

jsfiddle




回答2:


Adding another answer because the accepted answer did not work for me because I am using 1.7 and maybe someone else will find this useful.

Can be done by using _checkOffset method to alter as needed

$.datepicker._checkOffset_original = $.datepicker._checkOffset;
$.datepicker._checkOffset = function(inst, offset, isFixed) {
    var offset = $.datepicker._checkOffset_original(inst, offset, isFixed);
    var alterOffset = this._get(inst, 'alterOffset');
    if (alterOffset)
        return alterOffset.apply((inst.input ? inst.input[0] : null), [offset]);  // trigger custom callback
        return offset;
    }

and setting on the datepicker the wanted callback

$('#id').datepicker('option', 'alterOffset', function(offset){
    //change offset here
    return offset;
});


来源:https://stackoverflow.com/questions/14508389/jquery-datepicker-change-position-dynamically

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