Does anyone know how can I hide the value from the dropdown list with jQuery?
Example:
If I have a dropdown as following:
<select name="Test" id="Test">
<option value=""></option>
<option value="1">Test1</option>
<option value="2">Test2</option>
<option value="3">Test3</option>
<option value="4">Test4</option>
<option value="5">Test5</option>
<option value="6">Test6</option>
</select>
How can I hide the list value if the value is 2, 3, 4 in jQuery? So the output will be as below
<select name="Test" id="Test">
<option value=""></option>
<option value="1">Test1</option>
<option value="5">Test5</option>
<option value="6">T开发者_JAVA百科est6</option>
</select>
Assuming you don't want to get rid of them, the best you can do is disable them. I would guess that based on the same value that could hide them, changing that same value could potentially cause them to reappear. If that's the case you'd disable/enable them.
$('#Test').find('options[value=2], options[value=3]').attr('disabled', 'disabled');
If you wanna remove them...
$('#Test').find('options[value=2], options[value=3').remove();
I don't think you can hide those elements but you can remove them:
$('#Test').find('option[value=2], option[value=3], option[value=4]').remove();
function filter_select( dom, filter ){
var cache = $(dom).children('option').clone();
$(dom).children('option').each( function(){
var $this = $(this);
( filter.indexOf( Number( $this.val() ) ) > -1 ) && $this.remove();
});
return function undo(){
console.log('reattaching original options');
$(dom).empty().append(cache);
};
}
usage :
$(function(){
var u = filter_select('#Test', [2,3,4]);
// now you can call u() to undo the filter, if you want.
})
精彩评论