简体   繁体   English

第一次单击时不显示Datepicker

[英]Datepicker does not show at first click

I've just created a simple textbox where inside I binded the .click event to show the Datepicker , anyway, seems that at first click the Datepicker can't show, but if I click outside the control and then click again over the textbox I can see the Datepicker , I wonder why happen this? 我刚刚创建了一个简单的textbox ,在其中绑定了.click事件以显示Datepicker ,无论如何,似乎第一次单击时不会显示Datepicker ,但是如果我在控件外部单击,然后再次在textbox单击,可以看到Datepicker ,我想知道为什么会这样吗?

Check out my code here: 在这里查看我的代码:

<input type="text" id="calendar" placeholder="calendar">

$(function()
{
    $('#calendar').click(function()
    {
        $(this).datepicker();
    });
});

FIDDLE. 小提琴。

You are initializing Date picker on your text box click. 您正在初始化文本选择器,然后单击文本框。 You have to initialize on document ready method if you want to Open date picker immediate after page load 如果要在页面加载后立即打开日期选择器,则必须初始化文档就绪方法

Change your initializing code to 将初始化代码更改为

$(function()
{
    $('#calendar').datepicker();
});

For your reference fiddler updated 供您参考的提琴手已更新

You don't have to use the click event to start the date picker. 您不必使用click事件来启动日期选择器。 All you have to do is remove the click event and it will work! 您所要做的就是删除click事件,它将起作用! The date picker is already attached to your input box and will handle the events by itself. 日期选择器已附加到您的输入框中,并将自行处理事件。

Calling datepicker() does not invoke the calendar display. 调用datepicker()不会调用日历显示。 What datepicker() did was to attach a 'show calendar' event on input-focus. datepicker()所做的是在输入焦点上附加一个“显示日历”事件。 That's the reason why you see the calendar only when you put the focus back on the input field (not on every click of #calendar element). 这就是为什么只有将焦点重新放在输入字段(而不是每次单击#calendar元素)时才能看到日历的原因。

If you want to trigger the display of calendar due to some other event (like button click), you can call $('#calendar').datepicker('show') . 如果由于某些其他事件(例如按钮单击)而触发日历的显示,则可以调用$('#calendar').datepicker('show')

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

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