How to fix IE select issue when dynamically changing options

前端 未结 13 522
南旧
南旧 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条回答
  •  独厮守ぢ
    2020-12-04 16:41

    Adding couple of lines the following places (marked in bold as **) in render function of the selectDirective in angular.js worked fine for me. I am looking if there is any other possible solution other than patching angularJS or the forEach given below?

                if (existingOption.label !== option.label) {
                  lastElement.text(existingOption.label = option.label);
                  **lastElement.attr('label', existingOption.label);**
                }
    

    and

                  (element = optionTemplate.clone())
                      .val(option.id)
                      .attr('selected', option.selected)
                      .text(option.label);
                  **element.attr('label', option.label);**
    

    The issue was the label attribute of HTMLOptionElement is not the same as the text attribute if label is blank in IE.

    This can be seen verified by adding the following code after the screen has loaded and looking at the web console of FF and IE to see the difference. If you uncomment the last line where the label is set to text it works fine. Alternatively patch angular.js as above.

    // This is an IE fix for not updating the section of dropdowns which has ng-options with filters
    angular.forEach($("select"), function (currSelect) {
        console.log("1.text ", currSelect.options[currSelect.selectedIndex].text);
        console.log("1.label ", currSelect.options[currSelect.selectedIndex].label);
        //console.log("1.innerHTML ", currSelect.options[currSelect.selectedIndex].innerHTML);
        //console.log("1.textContent ", currSelect.options[currSelect.selectedIndex].textContent);
        //console.log("1.cN.data ", currSelect.options[currSelect.selectedIndex].childNodes[0].data);
        //console.log("1.cN.nodeValue ", currSelect.options[currSelect.selectedIndex].childNodes[0].nodeValue);
        //console.log("1.cN.textContent ", currSelect.options[currSelect.selectedIndex].childNodes[0].textContent);
        //console.log("1.cN.wholeText ", currSelect.options[currSelect.selectedIndex].childNodes[0].wholeText);
        //console.log("1. ", currSelect.options[currSelect.selectedIndex], "\n");
    
        //currSelect.options[currSelect.selectedIndex].label = "xyz";
        //currSelect.options[currSelect.selectedIndex].label = currSelect.options[currSelect.selectedIndex].text;
    });
    

提交回复
热议问题