jqueryUI datepicker在传递日期之前触发输入的模糊,避免/解决方法? [英] jqueryUI datepicker fires input's blur before passing date, avoid/workaround?

查看:109
本文介绍了jqueryUI datepicker在传递日期之前触发输入的模糊,避免/解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对文本输入进行了一些验证,该文本输入与其模糊事件相关联。我在这个字段上有一个datepicker(从jqueryUI的版本),所以当你点击该字段,datepicker显示,然后单击一个日期,并将日期填充到字段中作为datepicker。但是,在输入日期之前,似乎输入字段由于某种原因而变得模糊。在日期填充之前,它的焦点远离输入。所以我的验证在用户选择日期之前被触发,在日期实际上进入该字段之前,不应该这样做。它应该在日期被放入之后运行。有人知道为什么模糊是在这一点发生还是如何解决?

I have some validation on a text input bound to its blur event. I have a datepicker on this field (the version from jqueryUI), so when you click the field, datepicker shows up, then you click a date and it populates the date into the field as datepicker does. However, right before the date is entered it seems the input field gets its blur fired for some reason. It looks like focus goes away from the input before the date gets populated. So my validation gets fired right at the point when the user selects a date, before the date actually makes its way into the field, when it shouldn't. It should be running after the date gets put in. Does anyone know why the blur is is happening at that point or how to work around it?

推荐答案

当用户点击输入字段外部(选择日期)时,输入字段将模糊。没有办法。所以,而不是触发模糊验证,使用datepicker的onSelect回调。

When the user clicks outside the input field (to choose a date), the input field will blur. There's no way around that. So, instead of triggering the validation on blur, use the datepicker's onSelect callback.

$('.selector').datepicker({
    onSelect: function(dateText) { /* validation here */ }
});

如果要保留onblur事件,您可以推迟验证以允许datepicker填充在验证触发之前的字段,如下所示:

If you want to keep your onblur-events, you could postpone validation to allow for the datepicker to fill out the field before the validation fires, like this:

$('#myform input').blur(function () {
    setTimeout(function () { /* validation here */ }, 1);
});

使用setTimeout处理并发问题可能看起来像一个黑客,但由于JavaScript的单线程性质,它的工作相当不错。 jQuery-fame的John Resig在此博客中讨论此事。

Using setTimeout to handle concurrency-issues may look like a hack, but due to JavaScripts single-threaded nature, it works quite nicely. John Resig of jQuery-fame talks about it in this blogpost.

这篇关于jqueryUI datepicker在传递日期之前触发输入的模糊,避免/解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆