jQuery UI Datepicker onchange 事件问题
我有一个 JS 代码,当您更改字段时,它会调用搜索例程。问题是我找不到任何在 Datepicker 更新输入字段时触发的 jQuery 事件。
由于某种原因,当 Datepicker 更新字段时不会调用更改事件。当日历弹出时,它会改变焦点,所以我也无法使用它。有什么想法吗?
I have a JS code in which when you change a field it calls a search routine. The problem is that I can't find any jQuery events that will fire when the Datepicker updates the input field.
For some reason, a change event is not called when Datepicker updates the field. When the calendar pops up it changes the focus so I can't use that either. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您可以使用日期选择器的
onSelect
事件。实例:
不幸的是,只要选择日期,即使日期没有更改,
onSelect
也会触发。这是日期选择器中的一个设计缺陷:它总是触发onSelect
(即使没有任何更改),并且不会在发生更改时触发底层输入上的任何事件。 (如果您查看该示例的代码,我们正在监听更改,但它们没有被引发。)当事情发生变化时(可能是通常的更改 事件,或者可能是特定于日期选择器的事件)。
当然,如果您愿意,您可以在
input
上触发change
事件:这将在底层
input 上触发
对于通过 jQuery 连接的任何处理程序。但同样,它总是会触发它。如果您只想触发真正的更改,则必须保存先前的值(可能通过change
data
)并进行比较。实例:
You can use the datepicker's
onSelect
event.Live example:
Unfortunately,
onSelect
fires whenever a date is selected, even if it hasn't changed. This is a design flaw in the datepicker: It always firesonSelect
(even if nothing changed), and doesn't fire any event on the underlying input on change. (If you look in the code of that example, we're listening for changes, but they aren't being raised.) It should probably fire an event on the input when things change (possibly the usualchange
event, or possibly a datepicker-specific one).If you like, of course, you can make the
change
event on theinput
fire:That will fire
change
on the underlyinginput
for any handler hooked up via jQuery. But again, it always fires it. If you want to only fire on a real change, you'll have to save the previous value (possibly viadata
) and compare.Live example:
TJ Crowder 的回答(https://stackoverflow.com/a/6471992/481154)非常好,而且仍然准确。 在 onSelect 函数中触发更改事件已经非常接近了。
但是,日期选择器对象 (
lastVal
) 上有一个很好的属性,它允许您仅在实际更改时有条件地触发更改事件,而不必自己存储值:然后只需像平常一样处理更改事件:
T.J. Crowder's answer (https://stackoverflow.com/a/6471992/481154) is very good and still remains accurate. Triggering the change event within the onSelect function is as close as you're going to get.
However, there is a nice property on the datepicker object (
lastVal
) that will allow you to conditionally trigger the change event only on actual changes without having to store the value(s) yourself:Then just handle change events like normal:
您在 datepicker 对象中寻找 onSelect 事件:
Your looking for the onSelect event in the datepicker object:
我写这篇文章是因为我需要一个解决方案,仅在日期更改时才触发事件。
这是一个简单的解决方案。每次对话框关闭时,我们都会测试数据是否已更改。如果有,我们会触发自定义事件并重置存储的值。
现在我们可以为该自定义事件连接处理程序......
I wrote this because I needed a solution to trigger an event only if the date changed.
It is a simple solution. Each time the dialog box closes we test to see if the data has changed. If it has, we trigger a custom event and reset the stored value.
Now we can hook up handlers for that custom event...
我正在使用 bootstrap-datepicker,以上解决方案都不适合我。这个答案帮助了我..
bootstrap datepicker手动编辑日期或清除日期时,更改日期事件不会触发
这是我应用的内容:
希望这可以帮助其他人。
I am using bootstrap-datepicker and none of above solution worked for me. This answer helped me ..
bootstrap datepicker change date event doesnt fire up when manually editing dates or clearing date
Here is what I applied:
Hope this can help others.
尝试 :
Try :
我知道这是一个老问题。但我无法让 jquery $(this).change() 事件在 onSelect 上正确触发。因此,我采用以下方法通过 vanilla js 触发更改事件。
I know this is an old question. But i couldnt get the jquery $(this).change() event to fire correctly onSelect. So i went with the following approach to fire the change event via vanilla js.
试试这个
希望这有帮助:)
Try this
Hope this helps:)
在 jQueryUi 1.9 上,我设法通过附加数据值以及 beforeShow 和 onSelect 函数的组合来使其工作:
对我有用:)
On jQueryUi 1.9 I've managed to get it to work through an additional data value and a combination of beforeShow and onSelect functions:
Works for me :)
手册说:
所以:
对我有用。
Manual says:
So:
Works for me.
我的解决方案:
My soluthion:
我认为您的问题可能在于您的日期选择器的设置方式。
为什么不断开输入...不要使用 altField。相反,当 onSelect 触发时显式设置值。这将使您能够控制每次交互;用户文本字段和日期选择器。
注意:有时您必须在 .change() 而不是 .onSelect() 上调用例程,因为 onSelect 可能会在您不期望的不同交互上调用。
伪代码:
I think your problem may lie in how your datepicker is setup.
Why don't you disconnect the input... do not use altField. Instead explicitly set the values when the onSelect fires. This will give you control of each interaction; the user text field, and the datepicker.
Note: Sometimes you have to call the routine on .change() and not .onSelect() because onSelect can be called on different interactions that you are not expecting.
Pseudo Code:
DatePicker选中值改变事件代码如下
DatePicker selected value change event code below
如果您使用 wdcalendar 这将对您有所帮助,
onReturn 事件对我有用
希望这会有所帮助
if you are using wdcalendar this going to help you
the event onReturn works for me
hope this help
我的解决方案是:
My solution is:
你们中的一些人可能正在使用名为
XDSoft DateTimePicker
https://xdsoft.net/ jqplugins/datetimepicker/那里,更改事件是
https://xdsoft.net/jqplugins/datetimepicker/#onSelectDate
Some of you may be using something called
XDSoft DateTimePicker
https://xdsoft.net/jqplugins/datetimepicker/There, the change event is
https://xdsoft.net/jqplugins/datetimepicker/#onSelectDate
您可以使用
datepicker
的onClose
选项并将更改代码放入其中。当datepicker
模式关闭时触发。我尝试触发更改事件,但这不起作用,因此我将代码放在onClose
中,并且它起作用了。如果您需要在选择日期时触发,则同样适用于前面提到的onSelect
。日期选择器选项 https://api.jqueryui.com/datepicker/
You can use the
onClose
option for thedatepicker
and put your change code inside. This triggers when thedatepicker
modal is closed. I tried to trigger the change event but that wouldn't work so I put my code inside of theonClose
instead and it works. The same may work foronSelect
that was mentioned if you need to trigger when the date is selected.Datepicker options https://api.jqueryui.com/datepicker/
这种方法很简单,并且每次都可以与文本框控件一起使用。
$('#controlID').on('changeDate', function() { $(this).change(); });
This approach is simple and works everytime with textbox control.
$('#controlID').on('changeDate', function() { $(this).change(); });