How to fix IE select issue when dynamically changing options

前端 未结 13 532
南旧
南旧 2020-12-04 16:38

I have a set of selects that all have the same options. Then I run those options through a filter so that any options that are selected in a different select don\'t show up

13条回答
  •  萌比男神i
    2020-12-04 16:44

    On the lines of Mathew Berg answer, I modified it to work using AngularJS directive:

    angular.module('select',[]).directive("select", function() {
        return {
          restrict: "E",
          require: "?ngModel",
          scope: false,
          link: function (scope, element, attrs, ngModel) {
    
            if (!ngModel) {
              return;
            }
    
            element.bind("change", function() {
                //Fix for IE9 where it is not able to properly handle dropdown value change
                //The fix is to rip out the dropdown from DOM and add it back at the same location
                if (isIE9){
                    this.parentNode.insertBefore(this, this);   //this rips the elements out of the DOM and replace it into the same location.
                }
            })
          }
       }
    });
    

    This way the fix applies to all select elements in the project and you do not have to change any existing HTML markup. I also used the following method to detect IE version to set isIE9 variable to true:

    var Browser = {
        IsIE: function () {
            return navigator.appVersion.indexOf("MSIE") != -1;
        },
        Navigator: navigator.appVersion,
        Version: function() {
            var version = 999; // we assume a sane browser
            if (navigator.appVersion.indexOf("MSIE") != -1)
                // bah, IE again, lets downgrade version number
                version = parseFloat(navigator.appVersion.split("MSIE")[1]);
            return version;
        }
    };
    
    var oldIE = false;      //Global Variable
    var isIE9 = false;      //Global Variable
    if (Browser.IsIE && Browser.Version() <= 8) {
        oldIE = true;
    }
    
    if (Browser.IsIE && Browser.Version() == 9) {
        isIE9 = true;
    }
    

提交回复
热议问题