使用 jQuery inArray 并隐藏选项值
Using jQuery inArray and hiding an option value
我似乎无法在我的循环中隐藏某些选项值。目前,整个选项 select 值都被隐藏了。我怎样才能让它发挥作用?
function initialStrandOptions() {
if($('#component_id_1').prop('checked') == true){
var strand_options = $('.strand_options').find('option')
strand_options.each(function(){
if(jQuery.inArray(strand_options, ['4', '5', '6', '7'])){
$(this).hide();
}
});
}
}
是否还有更好的方法来实现我的目标?
您需要检查循环中this
引用的当前迭代选项的值,并将其与inArray
返回的-1
进行比较:
function initialStrandOptions() {
if ($('#component_id_1').prop('checked') == true) {
var strand_options = $('.strand_options').find('option')
strand_options.each(function () {
if (jQuery.inArray(this.value, ['4', '5', '6', '7']) > -1) {
$(this).hide();
}
});
}
}
我似乎无法在我的循环中隐藏某些选项值。目前,整个选项 select 值都被隐藏了。我怎样才能让它发挥作用?
function initialStrandOptions() {
if($('#component_id_1').prop('checked') == true){
var strand_options = $('.strand_options').find('option')
strand_options.each(function(){
if(jQuery.inArray(strand_options, ['4', '5', '6', '7'])){
$(this).hide();
}
});
}
}
是否还有更好的方法来实现我的目标?
您需要检查循环中this
引用的当前迭代选项的值,并将其与inArray
返回的-1
进行比较:
function initialStrandOptions() {
if ($('#component_id_1').prop('checked') == true) {
var strand_options = $('.strand_options').find('option')
strand_options.each(function () {
if (jQuery.inArray(this.value, ['4', '5', '6', '7']) > -1) {
$(this).hide();
}
});
}
}