嘿, 我是Mofei!
IE6 Select BUG: Unable to Set Selected Attribute, Undesignated Error

Recently, while working with JS select elements, I encountered a peculiar "unable to set selected attribute, undesignated error" in IE6. After searching on Google and other platforms, I found that many others have experienced the same bug. The solutions offered were almost all to add a setTimeout:

setTimeout(function(){
  selectObj.val(defaultSelect);// Select specified item
},0);

Upon closer inspection, I discovered a strange phenomenon: the selection had actually been completed before the JS threw the error. After the error was thrown, the JS code immediately stopped running. By using the above solution, although subsequent JS could be executed, using setTimeout to "delay execution" resolved the issue, it felt a bit "unreliable".

Since the error was thrown after the selection completed and did not affect the current selection, could I use a try-catch statement to capture the error? So I tried the following method:

try {
  selectMonth.val(oldMonth);// Select specified item
} catch (e) {
}

I found that it could indeed execute. However, upon further testing, I found that this try...catch only worked for up to 3 consecutive selections (I found it to be 3, but it may vary). Beyond this number, I still needed to use setTimeout, as shown below:

// Test of consecutive selections for year, month, and date
try {
  selectYear.val(oldYear);// First selection for year, can use try catch to capture error.
} catch (e) {
}

try {
    selectMonth.val(oldMonth);// Second consecutive selection, can still use this method.
} catch (e) {
}
// For the third consecutive selection, if I use try catch directly it may throw an error, so I added a setTimeout
setTimeout(function () {
    try {
        selectDate.val(oldDate);
     } catch (e) {
    }
}, 0);

Conclusion: Personally, I feel that using try catch for handling this issue is slightly better than using setTimeout alone. However, the root cause of this error still needs to be considered. My guess is that there might be a slight delay in loading the options within the select, giving it an asynchronous feel, ultimately leading to the aforementioned bug. Of course, if anyone knowledgeable knows the "truth" behind this, please feel free to enlighten me.

THE END
Previous: It's Here!

More Articles You Might Be Interested In

I had so many questions while writing this—what’s your take?

avatar

Mofei's Friend (Click to edit)

I heard leaving a comment here is pretty cool.

HI. I AM MOFEI!

NICE TO MEET YOU!