简体   繁体   English

JQuery-UI Datepicker()click事件似乎与自定义事件冲突

[英]JQuery-UI Datepicker() click event seems to conflict with a custom event

I'm a bit confused by some behavior on the frontend. 我对前端的某些行为感到困惑。 Im using the datepicker() method from the JQuery UI libraries, and when a date is first selected, I want the calendar to slide left to display the rest of a form. 我使用了JQuery UI库中的datepicker()方法,当第一次选择日期时,我希望日历向左滑动以显示表单的其余部分。 That works fine unless the previous/next month elements are clicked first. 除非先单击上一个/下一个月元素,否则该方法很好。 Then the DOM changes(possibly unrelated) and nothing else happens. 然后DOM发生变化(可能不相关),然后什么也没有发生。 The calendar stays in its original position and the form never shows up. 日历保持其原始位置,并且表格永远不会显示。 It's like the event listener that I wrote is being overridden. 就像我编写的事件侦听器被覆盖一样。 I'm assuming I just have a fundamental misunderstanding of event handling with javascript/jquery. 我假设我对javascript / jquery事件处理有一个基本的误解。 Im a backend developer with only the slightest frontend experience, so if anyone could explain to me what im doing wrong, I'd be greatly appreciative. 我是只有一点点前端经验的后端开发人员,所以如果有人可以向我解释自己在做什么错,我将不胜感激。

Here's a super simplified example that creates the same behavior: 这是一个创建相同行为的超级简化示例:

HTML: HTML:

<div id="calendar"></div>

CSS: CSS:

#calendar {
    position:relative;
    left:10em; }

JS: JS:

$(document).ready(function () {  
    $("#calendar").datepicker();  
    $("#calendar tbody td").click(function () {  
        $("#calendar").animate({  
            left: "-30px",  
            easing: 'swing'  
        });  
    });  
});  

And here is a fiddle: http://jsfiddle.net/HM9ea/ 这是一个小提琴: http : //jsfiddle.net/HM9ea/

Try this with onSelect : 尝试使用onSelect

$("#calendar").datepicker({
    onSelect: function () {
        $(this).animate({
            left: "-30px",
            easing: 'swing'
        });
    }
});

Demo 演示


Issue with your code is that you were trying to animate a object which was not presenet at the state of document ready. 您的代码的问题在于,您试图为在文档就绪状态下未预置的对象设置动画。 so your event does not get fired, instead you can use the builtin method of select onSelect . 因此您的事件不会被触发,而是可以使用select onSelect的内置方法。

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

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