How to prevent jquery datepicker from popping up on initial page load but still scroll to datepicker textbox

房东的猫 提交于 2020-01-16 01:20:16

问题


I have something similar to the following javascript:

$(function () {
    $("#txtDate").focus();
    $("#txtDate").datepicker({
        ...
        autoOpen: false,
        ...
    });
});

I want to focus in on the textbox on page load without having the datepicker pop up. Currently the page loads with the datepicker already open.

I need to be able to have the page scroll to that textbox on page load and it would be nice for the txtDate textbox to have focus so that users can tab to the next input in the form easily.

Any help would be greatly appreciated.

EDIT:
NOTE: must work in IE 8-10


回答1:


Try this:

var initialized = false;
$(function () {
    $("#txtDate").focus();
    $("#txtDate").blur(function(){
        if(!initialized){
            $("#txtDate").datepicker({
                autoOpen: false
            });    
            initialized = true;
        }
    });
});

Example

http://jsfiddle.net/pQALk/3/




回答2:


This jQuery works:

$(function () {
    $(".txtDate").focus();
    setTimeout(function() {$(".txtDate").datepicker({
        autoOpen: false
    });},10);
});

It's a bit of a hack - added a delay of 0.01 seconds before binding the datepicker, so the focus happens before the datepicker is bound and so doesn't trigger the datepicker. http://jsfiddle.net/pQALk/2/



来源:https://stackoverflow.com/questions/20983172/how-to-prevent-jquery-datepicker-from-popping-up-on-initial-page-load-but-still

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