简体   繁体   English

通过远程按钮激活JQuery UI日期选择器

[英]Activating a JQuery UI datepicker via remote Button

I'm using the JQuery UI datepicker , but instead of attaching the datepicker to an input[type=text] element, i'm attaching it to an input[type=hidden] element. 我正在使用JQuery UI datepicker ,但是我没有将datepicker附加到input[type=text]元素,而是将它附加到input[type=hidden]元素。 In order to activate the datepicker, I'm using a remote button on the UI that's not a sibling of the input element the datepicker is attached to. 为了激活datepicker,我在UI上使用了一个远程按钮,它不是datepicker附加到的输入元素的兄弟。

In order to show the datepicker, I'm using a click event handler which executes this code 为了显示datepicker,我使用了一个执行此代码的click事件处理程序

$('input.trial_end_date').datepicker('show');

This works to show the datepicker, but whenever I click a date, I get an uncaught javascript exception: 这可以显示datepicker,但每当我点击一个日期时,我都会得到一个未被捕获的javascript异常:

Uncaught Missing instance data for this datepicker jquery-ui-1.10.1.custom.min.js:6
e.extend._getInst jquery-ui-1.10.1.custom.min.js:6
e.extend._selectDay jquery-ui-1.10.1.custom.min.js:6
t.selectDay jquery-ui-1.10.1.custom.min.js:6
v.event.dispatch localhost.local/:5
o.handle.u localhost.local/:5

However, if I change the input[type=hidden] to an input[type=text] to allow the datepicker to show when the input element is focused, all click events fire without any problems (until I activate the datepicker via the button, then they all fail no matter which way the datepicker is activated). 但是,如果我将input[type=hidden]更改为input[type=text]以允许datepicker显示何时输入元素被聚焦,则所有单击事件都会触发而没有任何问题(直到我通过按钮激活datepicker,然后无论以何种方式激活日期选择器,它们都会失败)。

Is there a more reliable way to activate the date picker via a remote button and hide the input element for the date? 是否有更可靠的方法通过远程按钮激活日期选择器并隐藏日期的输入元素? I'm not sure why this exception is being thrown at all. 我不确定为什么会抛出这个异常。

Here's an example of this not working in a JSFiddle . 这是一个不能在JSFiddle中工作的例子 Though, the exception isn't the same. 但是,例外是不一样的。 After further research, I seem to have found the reason, which I'll add in an answer below. 经过进一步的研究,我似乎找到了原因,我将在下面的答案中加入。

Why would you use a hidden input when you can instantiate a datepicker on a regular div and just hide and show that div? 当您可以在常规div上实例化一个日期选择器并隐藏并显示该div时,为什么要使用隐藏输入? If you don't need the input, don't use it. 如果您不需要输入,请不要使用它。

<input type="button" id="open" />
<div id="datepicker"></div>

JS JS

$('#datepicker').datepicker();

$('#open').on('click', function() {
    $('#datepicker').toggle();
});

FIDDLE 小提琴

After messing around with this problem a bit in a JSFiddle, ( http://jsfiddle.net/XJ7Z7/1/ ), it's apparent that if you attach the datepicker to an input element - the input element must be visible (ie, not a hidden element). 在JSFiddle( http://jsfiddle.net/XJ7Z7/1/ )中稍微解决了这个问题之后,很明显,如果将datepicker附加到输入元素 - 输入元素必须是可见的(即,不是一个隐藏的元素)。

The reason for this is because the Datepicker module tries to position the calendar via position:absolute; 原因是因为Datepicker模块尝试通过position:absolute;来定位日历position:absolute; and to do so accurately needs to get the top/left offset for the input element to which it is attached. 并且要准确地这样做需要获得它所连接的输入元素的顶部/左侧偏移量。 A hidden input element has no top/left offset (it's set to 0/0), so therefore the module skips that element to try to find one that is visible that it can use to calculate where to place the calendar. 隐藏的输入元素没有顶部/左侧偏移(它设置为0/0),因此模块跳过该元素以尝试找到可用于计算日历放置位置的元素。

If there's no sibling element that can be used the reference object gets set to null and an exception is thrown when the datepicker tries to access the left/top properties of the return object of $(el).offset() because the call to offset returns a null value. 如果没有可以使用的兄弟元素,则引用对象被设置为null,并且当datepicker尝试访问$(el).offset()的返回对象的left / top属性时会抛出异常,因为对offset的调用返回null值。

The solution here is outlined by @adeneo in the accepted answer, but the sacrifice is that you'll have to manage your hidden input value for the selected date on your own via the onSelect callback of the datepicker module initialization. 这里的解决方案由@adeneo在接受的答案中概述,但牺牲的是你必须通过datepicker模块初始化的onSelect回调来自行管理所选日期的隐藏输入值。

I hope this helps someone out there who is running into a similar problem. 我希望这可以帮助那些遇到类似问题的人。

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

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