如何使用焦点启动 jquery datepicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start jquery datepicker with focus
提问by jim
I'm new to jquery and would like to start datepicker with focus.
我是 jquery 的新手,想开始重点关注 datepicker。
My first textbox is a date field and I have tried to give the box focus with javascript but datepicker won't come up unless I click somewhere else on the page and then give it focus by clicking inside the box.
我的第一个文本框是一个日期字段,我尝试使用 javascript 为框设置焦点,但除非我单击页面上的其他位置,然后通过单击框内的按钮来设置焦点,否则日期选择器不会出现。
Is there a way to start datepicker with focus and maybe have the widget start immediately when the page loads then drop focus when the user leaves the box?
有没有办法用焦点启动日期选择器,也许让小部件在页面加载时立即启动,然后在用户离开框时放下焦点?
$( "#date" ).datepicker({
dateFormat: "mm-dd-yy"
});
回答by Hari Pachuveetil
Try this - http://www.jsfiddle.net/wnUWQ/embedded/result
试试这个 - http://www.jsfiddle.net/wnUWQ/embedded/result
$(document).ready(function() {
$("#datepick").datepicker({
dateFormat: "mm-dd-yy"
});
$("#datepick").focus(function() {
$("#datepick").datepicker("show");
});
$("#datepick").focus();
});
EDIT:
The .ready()
function of $(document) object is fired when the DOM is completely loaded in the browser. First we attach the datepicker
to the input
, and then we attach a focus
eventhandler that shows the datepicker and last we set the focus on to the input.
编辑:.ready()
当 DOM 完全加载到浏览器中时,将触发 $(document) 对象的函数。首先我们将 附加datepicker
到input
,然后我们附加一个focus
显示日期选择器的事件处理程序,最后我们将焦点设置在输入上。
This could all be chainedinto one line as in:
这可以全部链接成一行,如下所示:
$(document).ready(function() {
$("#datepick").datepicker({
dateFormat: "mm-dd-yy"
}).focus(function() {
$("#datepick").datepicker("show");
}).focus();
});