簡體   English   中英

使用jQuery inArray並隱藏選項值

[英]Using jQuery inArray and hiding an option value

我似乎無法在循環中隱藏某些選項值。 此刻,整個選項選擇值都被隱藏了。 我該如何運作?

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在循環,並且還具有比較-1被返回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();
            }
        });
    }
}

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM