jQuery not allowing to set an attribute as 'selected'
I am using jQuery 1.3.2 and I am running into exactly the same problem mentioned here.
var tmp = jQuery(selector_string);
tmp.attr("selected", "selected");
I am getting value开发者_JAVA百科 of tmp correctly in IE6. However setting the attribute to 'selected' is failing. It works in IE7 and in Firefox.
Another person having the same problem.
Are you sure the DOM element you're trying to set is a dropdwon? Or another element that can indeed be 'selected'?
I've had success when dealing with dropboxes like this in IE6 with just setting the selectedIndex
property on the parent select
element, rather than trying to tell an individual option to be "selected".
$('select#mySelector')[0].selectedIndex = 3; // or whatever
You could determine an individual option
's index by just counting his prev siblings
var si = $('option#myOption).prevAll('option').length;
It was interesting to see in the second link you included, that a solution was given suggesting you inject a slight delay between manipulating the DOM and trying to do what you want. I'm guessing IE6's rendering/javascript engine must rely on the element being visible on the page before allowing this action. (The settimeout would allow this occur.)
Good luck!
Hard to verify without IE6, but perhaps val()
could be changed to value === ''
? Not sure if that is compatible.
Also, perhaps .attr("selected", true)
?
精彩评论