简体   繁体   中英

Using jQuery inArray and hiding an option value

I can't seem to be able to hide certain option values within my loop. At the moment the whole option select values get hidden. How can I make it work?

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();
    }
   });  
 }
}

Is there a better way also of achieving my goal?

You need to check value of the currently iterated option referenced by this in the loop, and also compare it with -1 returned by inArray :

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();
            }
        });
    }
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM