简体   繁体   English

Datepicker弹出第一个焦点

[英]Datepicker pops up on first focus

The first input field of my form gains focus after a few miliseconds after document ready (in my case up to 50 ms). 我的表单的第一个输入字段在文档就绪后几毫秒(在我的情况下最多50毫秒)后获得焦点。 Then the datepicker pops up in case the first input field is a date. 然后弹出日期选择器,以防第一个输入字段是日期。

What I need is to have focus on the first input field after the page loads and pop up the datepicker on focus event, but not immediately after the page loads. 我需要的是在页面加载后关注第一个输入字段并在焦点事件上弹出datepicker,但不会在页面加载后立即弹出。 Which means if the first input element is a date, it gains focus and after I click on it the datepicker should show itself. 这意味着如果第一个输入元素是一个日期,它会获得焦点,在我点击它之后,datepicker应该显示自己。 Then all the other datepickers should pop up on focus. 然后所有其他的日期选择器应该弹出焦点。 I need to do it that way so I wont have to loose and gain focus again. 我需要这样做,所以我不必松开并再次获得焦点。

<input data-calendar-format="dd.MM.yyyy">

$(document).ready(function() {
    $('input[data-calendar-format]').datepicker({
        dateFormat: 'dd.mm.yy'
    });
});

You can use the beforeShow option to prevent the datepicker from opening. 您可以使用beforeShow选项来阻止打开datepicker If the page just loaded return false . 如果刚刚加载的页面return false I have used time to determine whether the page just loaded; 我用时间来确定页面是否刚加载; if you're going to use time find a time that suits your needs. 如果你打算用时间找一个适合你需要的时间。

 $(document).ready(function() { var dt = Date.now(); $('input[data-calendar-format]').datepicker({ dateFormat: 'dd.mm.yy', beforeShow: function() { if( Date.now() - dt < 51 ) { return false; } } }); $(':input').first().focus().on('click',function() { $(this).datepicker('show'); }); }); 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <input data-calendar-format="dd.MM.yyyy"> 

You can change the showOn property on the first datepicker to button , hide the button in your css and add an autofocus to your input field. 您可以将第一个showOn上的showOn属性更改为button ,隐藏css中的按钮并将autofocus添加到输入字段。 Adding an additional click handler to the first input will help to open the datepicker, before clicking anywhere else. 在第一个输入中添加额外的click处理程序将有助于打开日期选择器,然后单击其他任何位置。

 $(document).ready(function() { $('input[data-calendar-format]').datepicker({ dateFormat: 'dd.mm.yy', showOn: "button" }); $('.more-datepickers').datepicker({ dateFormat: 'dd.mm.yy', showOn: "focus" }); $('input[data-calendar-format]').on('click', function() { $(this).datepicker("show"); }) }); 
 .ui-datepicker-trigger { display: none; } 
 <link href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <input data-calendar-format="dd.MM.yyyy" autofocus /> <input class="more-datepickers"> <input class="more-datepickers"> 

Not ideal, but you could have two initializations: one for all the datepickers, and another for the first datepicker (if it's the first element): 不理想,但你可以有两个初始化:一个用于所有的datepickers,另一个用于第一个datepicker(如果它是第一个元素):

$(document).ready(function() {

    // all the datepickers that are not the first child
    $('input[data-calendar-format]:not(:nth-child(1))').datepicker({
        dateFormat: 'dd.mm.yy'
    });

    // the first child if it's a datepicker
    $('input[data-calendar-format]:nth-child(1)').focus().datepicker({
        dateFormat: 'dd.mm.yy'
    }).on("click", function() {
        // the field is focused, so we need to trigger datepicker to show the first time
        $(this).datepicker("show");
    });

});

Update: If the field is focused, you'll need to trigger the datepicker to show the first time. 更新:如果该字段是焦点,则需要触发datepicker以便第一次显示。 Added a click event to deal with that. 添加了一个click事件来处理它。

You can see it working on this JSFiddle: http://jsfiddle.net/sqjgk8y7/1/ 你可以看到它在这个JSFiddle上工作: http//jsfiddle.net/sqjgk8y7/1/

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

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