trying to hide options from selectlist .. not working on chrome and ie
I have a select lists, which has lots of option. Depending on some input I want to hide few options from select list. To hide options from select list I have written jquery like
$('#selectlist1 option').each(function(){
$(this).hide();
})
But this code seems to work only for firefox and its not working on chrome and ie. Whereas if I write
$('#selectlist1').hide();
it works for all browser. Any pointer where should I l开发者_开发知识库ook at?
Here's a relatively concise way to rebuild the select list on demand with new options. This works for dynamically inserted options (which is what IE and Chrome have a problem with showing and hiding)
$().ready(function() {
//store a reference
var select = $('#myselect');
});
function rebuild_select(arr_new_options) {
var parent = select.parent();
select.empty();
var clone = select.clone();
select.remove();
for(var x=0; x < arr_new_options.length; x++) {
clone.append(arr_new_options[x]);
}
parent.append(clone);
select = clone;
}
You cannot hide individual option elements x-browser. The only solution is to replace the select with a new element with only the options you wish to display.
See this other question
For anyone having to deal with hiding option elements in those versions affected, I posted a workaround here which doesn't clone or remove the options but wraps spans around them, which is arguably much easier to deal with:
http://work.arounds.org/issue/96/option-elements-do-not-hide-in-IE/
精彩评论