简体   繁体   English

在日期选择器中选择日期会触发错误的事件

[英]Selecting date in datepicker fires wrong event

I have two elements on my website 我的网站上有两个元素

<asp:DropDownList CssClass="SelectionDDL" ID="ddlClientMapFilter" runat="server"></asp:DropDownList>

and somewhere else on the page 以及页面上的其他地方

<asp:TextBox ID="tbFromDate" runat="server" OnTextChanged="ddl_ReportFilterChanged"></asp:TextBox>
<asp:TextBox ID="tbToDate" runat="server" OnTextChanged="ddl_ReportFilterChanged"></asp:TextBox>

In javascript in 在javascript中

$(function(){
    $(document).on("change", $("#ddlClientMapFilter"), function() {
         RefreshMapList();
    });
});

and somewhere else on page in Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(OnAsychPostback); 以及Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(OnAsychPostback);页面上的其他Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(OnAsychPostback);

function OnAsychPostback(s, e) {
$("#tbFromDate").datepicker({
            defaultDate: "-1",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function (selectedDate) {
                $("#tbToDate").datepicker("option", "minDate", selectedDate);
            }
        });
        $("#tbToDate").datepicker({
            //defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function (selectedDate) {
                $("#tbFromDate").datepicker("option", "maxDate", selectedDate);
            }
        });
}

Now the strange part. 现在奇怪的部分。

Everything seems to be working except that every time I select a date using any of the datepickers the dropdown event gets fired. 一切似乎都正常,除了每次我使用任何日期选择器选择日期时都会触发下拉事件。 I know the solution is simple add if and check were there the target of the event is that dropdown but I'd like to know how to not fire that event, also I have tons of other events attached next to that one and only this one is fired. 我知道解决方案很简单,添加并检查事件的目标是否为下拉列表,但是我想知道如何不触发该事件,此外,在该事件旁边还有很多其他事件,并且只有这个事件被解雇了。

Apparently there is a bug somewhere but I don't know how to find it any ideas? 显然某个地方有一个错误,但我不知道如何找到它的任何想法?

it is because the second argument to on() (in delegation syntax) must be a selector string, not a jQuery object 这是因为on()的第二个参数(使用委托语法)必须是选择器字符串,而不是jQuery对象

$(function(){
    $(document).on("change", "#ddlClientMapFilter", function() {
         RefreshMapList();
    });
});

Demo: Fiddle 演示: 小提琴

In the demo the first handler is fired only when the #x element is changed, but the second handler is fired when any input is changed. 在演示中,仅在#x元素更改时才触发第一个处理程序,而在任何输入更改时都将触发第二个处理程序。

The root cause seems to be that if the second argument is not a string, then it is considered as the data parameter(you can check the data property of the event object to test this)... 根本原因似乎是,如果第二个参数不是字符串,则将其视为数据参数(您可以检查事件对象的data属性以对此进行测试)...

Demo: Fiddle 演示: 小提琴

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

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