Failed to set the selected jQuery property. Unpacking jQuery

The following code will not work for me in ie6, but it is not surprising that it works in any other browser:

$ ('# myDropdown') Val ('value') ;.

I have no idea why this is happening.

Any thoughts?

+3
source share
3 answers

You probably need to do something like

$("#myDropdown > option[selected]").removeAttr("selected");
$("#myDropdown > option[value='value']").attr("selected","selected");

Just to guess.

+3
source

I had to set innerHTML to select.

I came up with the following extension method, which has a special routine for IE6:

$.fn.setDropDownValue = function(valueToSet) {
    if ((!this[0].options) || (this[0].options.length == 0))
        return;

    if ($.browser.msie && $.browser.version == 6.0) {
        var open = '<OPTION value=\'';
        var html = '';
        for (var i = 0; i < this[0].options.length; i++) {
            var opt = $(this[0].options[i]);
            html += open + $(opt).val() + '\'';
            if (opt.val() == valueToSet) {
                html += ' selected';
            }
            html += '>' + opt.html() + '</OPTION>';
        }
        $(this).html(html);
    } else {
        this.val(valueToSet);
    }
};
+2
source

IE 6. , - :

options[1].setAttribute('selected', true)

-, :

setTimeout("$('#myDropdown').val('" + value + "')",1);

try/catch - IE6.

+1

Source: https://habr.com/ru/post/1736521/


All Articles