IE6中 “無法設置selected屬性”的腳本錯誤提示


在IE6下,彈出腳本錯誤的提示:無法設置selected屬性 未指明的錯誤

代碼:selectObj.val(defaultSelect);//選中指定項

出現場景:動態填充select標籤:爲其添加option選項,之後設置默認項時出現
後期測試:發現並不一定出現,非常怪異,應該是還沒有完全明白髮生這個問題的時機

解決辦法:

在設置selected屬性的時候使用setTimeout函數設定一個任意時間即可
setTimeout(function(){
     selectObj.val(defaultSelect);//選中指定項
},0);

IE 6 can throw an error "Error: Could not set the selected property. Unspecified error." when you try to manipulate select elements by adding options and then selecting one of your newly created items. I came across this while trying to use jQuery's .val() function to set the selected element after I had added some options to my select.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章