使用JavaScript隐藏选择选项
我有下面的选择下拉列表使用JavaScript隐藏选择选项,javascript,Javascript,我有下面的选择下拉列表 <select id="strand_id" class="strand_options" name="strand[id]"> <option value="1">Text Value</option> <option value="2">Text Value</option> <option value="3">Text Value</option> <optio
<select id="strand_id" class="strand_options" name="strand[id]">
<option value="1">Text Value</option>
<option value="2">Text Value</option>
<option value="3">Text Value</option>
<option value="4">Text Value</option>
<option value="5">Text Value</option>
<option value="6">Text Value</option>
<option value="7">Text Value</option>
</select>
我想我需要做的是迭代每个strand_数组值,然后说
"if option values are 4, 5, 6, or 7 then hide them"
所以我一直在看indexOf()
,但似乎无法正确使用它,我可以使用jQuery.inArray()
很好,但我在这里挣扎(可能太明显了?)
如果我使用jQuery,我会这样做
var strand_options = $('.strand_options').find('option');
strand_options.each(function(){
if( jQuery.inArray($(this).val(), ['4', '5', '6', '7']) != -1 ){
$(this).hide();
}
});
那么,我从哪里得不到这个呢
我没有正确地迭代串数组吗?使用删除而不是隐藏
var strand_options = $('.strand_options').find('option');
strand_options.each(function(){
if( jQuery.inArray($(this).val(), ['4', '5', '6', '7']) != -1 ){
$(this).remove();
}
});
声明要隐藏的值数组:
var toHide = ['4', '5', '6', '7'];
然后遍历以下选项:
var sel = document.getElementById("strand_id");
for (var i = 0; i < sel.length; ++i)
if (toHide.indexOf(sel.options[i].value) >= 0)
sel.options[i].style.display = "none";
var sel=document.getElementById(“strand_id”);
对于(变量i=0;i=0)
sel.options[i].style.display=“无”;
请试试这个
var $strand_options = $('.strand_options').find('option');
$strand_options.each(function () {
if (jQuery.inArray($(this).val(), ['4', '5', '6', '7']) != -1) {
$(this).hide();
}
});
谢谢,但我知道如何使用jQuery,事实上我发布的内容可以使用jQuery。问题不是使用jQuery,而是使用JavaScript
var $strand_options = $('.strand_options').find('option');
$strand_options.each(function () {
if (jQuery.inArray($(this).val(), ['4', '5', '6', '7']) != -1) {
$(this).hide();
}
});