Jquery datepicker popup not closing on select date in IE8

前端 未结 10 1353
逝去的感伤
逝去的感伤 2020-12-15 05:46

I\'ve got a web form with a start date field. I\'ve tied a jquery datepicker to the txt field. Now when I choose a date in FF, the selected date is populated in the text box

10条回答
  •  情书的邮戳
    2020-12-15 06:32

    The reason

    The root bug (I think it's probably meant to be a feature, or maybe a workaround for a known IE bug?) is in ASP.Net's ValidatorHookupEvent:

    var func;
    if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
        func = new Function(functionPrefix + " " + ev);
    }
    else {
        func = new Function("event", functionPrefix + " " + ev);
    }
    

    As a result, in the default case that there's no other onchange registered, this sets up the onchange for the input to be the equivalent of

    function() { ValidatorOnChange(event); }
    

    in IE and

    function(event) { ValidatorOnChange(event); }
    

    in other browsers. So iff you're using IE, the event passed to ValidatorOnChange will be window.event (since window is the global object).

    A suggested solution

    If you don't want to hack around with the ASP.Net scripts then I think the nicest way to handle this is to detect the broken event handler and replace it. As with other suggestions here, I offer an onSelect to include in the datepicker options.

    onSelect: function(dateStr, datePicker) {
        // http://stackoverflow.com/questions/1704398
        if (datePicker.input[0].onchange.toString().match(/^[^(]+\(\)\s*{\s*ValidatorOnChange\(event\);\s*}\s*$/)) {
            datePicker.input[0].onchange = ValidatorOnChange;
        }
        datePicker.input.trigger("change");
    }
    

    I'm applying this globally with

    $.datepicker.setDefaults({
        onSelect: function(dateStr, datePicker) {
            etc.
        }
    });
    

提交回复
热议问题