试图从选择列表中隐藏选项..不适用于chrome和ie [英] 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();
})
但是此代码似乎仅适用于firefox,不适用于chrome和ie.而如果我写
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 look at?
推荐答案
这是一种相对简洁的方法,可以使用新选项按需重建选择列表.这适用于动态插入的选项(这是IE和Chrome在显示和隐藏时遇到的问题)
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;
}
这篇关于试图从选择列表中隐藏选项..不适用于chrome和ie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!