Jquery datepicker popup not closing on select date in IE8

前端 未结 10 1292
逝去的感伤
逝去的感伤 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:33

    I don't really know what the problem is, but I did notice that your does not have the ValidationGroup set, and both of your validators have that value set. You might try setting ValidationGroup="sh" in your TextBox and see if that helps.

    0 讨论(0)
  • 2020-12-15 06:35

    Building upon the above answers and to provide further detail to my comment above,

    Apparently in IE9+ and other browsers, you should now use dispatchEvent to fire the change event. (Why does .fireEvent() not work in IE9?)

    The OnSelect function in the datepicker actually has 2 arguments:

    1. The value of the textbox associated with the datepicker
    2. An object with an id property that matches that of the textbox

      mytextbox.datepicker({
          defaultDate: null,
          numberOfMonths: 1,
          dateFormat: DisplayDateFormat,
          onSelect: function (value, source) {
          }
      });
      

    All the examples I saw used document.getElementById() to retrieve the textbox, which I thought wouldn't be necessary seeing as the source object has the same id as the textbox. Upon closer examination it turns out that source is an object, not the textbox element. I found the following code resolved the problem though:

        mytextbox.datepicker({
            defaultDate: null,
            numberOfMonths: 1,
            dateFormat: DisplayDateFormat,
            onSelect: function (value, source) {
    
                var ctrl = document.getElementById(source.id);
    
                if ("dispatchEvent" in ctrl) {
    
                    // IE9
                    var evt = document.createEvent("HTMLEvents");
                    evt.initEvent("change", false, true);
                    ctrl.dispatchEvent(evt);
                } else if ("fireEvent" in ctrl) {
    
                    // IE7/IE8
                    ctrl.fireEvent("onchange");
                } else {
    
                    $(ctrl).change();
                }
            }
        });
    

    Update: It appears that this approach is no longer working - not sure why. It stops the error from being thrown but doesn't trigger the change event.

    0 讨论(0)
  • 2020-12-15 06:38

    As a date is selected, the datepicker triggers the change event on the INPUT element, but the ASP.Net validator picks up the click event instead, with the source an A element, and tries to find the validators on that A element, instead of the INPUT. This can be observed by inspecting event.srcElement inside the validator's ValidatorOnChange function. In browsers other than IE, event.type is 'change' and event.target is correctly the INPUT.

    While the no-op function onSelect: function() { } prevents the error, by overriding the .change() built-in to the datepicker's default onSelect, it also prevents the validators from triggering. Here's a work-around for both:

    onSelect: function() {
      this.fireEvent && this.fireEvent('onchange') || $(this).change();
    }
    

    This uses the normal .change() trigger except on IE, where it's required to use .fireEvent to get the event object to be associated with the change and not the click.

    0 讨论(0)
  • 2020-12-15 06:44

    It doesn't look like you're doing anything wrong since the ValidatorOnChange code is generated for you; there's something wrong in the way it's creating its vals object which appears to end up null on ie8.

    It's been asked before, and the solution is overriding the onSelect function with a no-op function.

    This is not the only kind of validator problem out there. Here's a vaguely similar issue with the autocomplete feature.

    0 讨论(0)
提交回复
热议问题