简体   繁体   English

如何防止 jQuery datepicker 在初始页面加载时弹出但仍滚动到 datepicker 文本框

[英]How to prevent jQuery datepicker from popping up on initial page load but still scroll to datepicker textbox

I have something similar to the following Javascript:我有类似于以下 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.我需要能够在页面加载时将页面滚动到该文本框,并且txtDate文本框具有焦点会很好,以便用户可以轻松地切换到表单中的下一个输入。

Any help would be greatly appreciated.任何帮助将不胜感激。

EDIT:编辑:
NOTE: must work in IE 8-10注意:必须在 IE 8-10 下工作

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/ http://jsfiddle.net/pQALk/3/

This jQuery works:这个 jQuery 有效:

$(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.这有点黑客 - 在绑定日期选择器之前添加了 0.01 秒的延迟,因此焦点发生在日期选择器绑定之前,因此不会触发日期选择器。 http://jsfiddle.net/pQALk/2/ http://jsfiddle.net/pQALk/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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