开发者

Adding select element through javascript doesn't work in ie

开发者 https://www.devze.com 2023-03-01 16:42 出处:网络
my code works fine in Safari, and FF, but in IE the drop down is empty.Its just empty.Any ideas?This is jquery-1.5.Thanks!

my code works fine in Safari, and FF, but in IE the drop down is empty. Its just empty. Any ideas? This is jquery-1.5. Thanks!

var sel = document.createElement("select");
sel.setAttribute("id", key)
sel.setAttribute('name', key)
for (var option in ddHash[key]){
    var optElement = document.createEl开发者_Go百科ement("option")
    optElement.text = ddHash[key][option]
    if (// some conditional){
        optElement.selected = true
    }
    else {
        optElement.selected = false
    }
    sel.appendChild(optElement)
}


I believe optElement.text should be optElement.innerText for IE.


From the MSDN reference for the option object:

You can create new OPTION elements dynamically with the document.createElement method, but you cannot change properties until the new element is added to a SELECT object. Or, you can create fully formed elements by using the Option object, as follows:

var opt = new Option( 'Text', 'Value', fDefaultSelected );

So it seems you just need to call appendChild before trying to set the properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号