2016-04-18 4 views
0

私はこの単純化されたHTMLの構造を有する:複数をチェックする方法選択したかどうかを選択する

<div id="kurir_list"> 
    <div class="row"> 
     <div class="col-sm-6"> 
       <select disabled class="tarif"> 
       ... 
       </select> 
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-sm-6"> 
       <select disabled class="tarif"> 
       ... 
       </select> 
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-sm-6"> 
       <select disabled class="tarif"> 
       ... 
       </select> 
      </div> 
     </div> 
    </div> 
</div> 

をし、私もこのjqueryのを持っている:

$(".tarif").change(function() { 
    if (!$(".tarif").is(':disabled')) { 
     alert ("hello world"); 

     if ($(".tarif").val() !== "") { 
      alert ("hello earth"); 
     } 
    } 
}); 

なぜ「ハロー地球は」すべての.tarifオプションを選択しているにもかかわらず現れることはありませんか? .tarifに空の選択があるかどうかを検証する方法は?ありがとう

答えて

1

.filter()メソッドをlengthプロパティで使用できます。

//Cache elements 
var tarif = $(".tarif"); 

tarif.change(function() { 
    //Filter not disabled elements 
    var notDisabledTarif = tarif.filter('not(:disabled)'); 

    //There is atleast 1 element which is not disabled 
    if(notDisabledTarif.length){ 
     alert ("hello world"); 

     //Filter elements having value 
     var elementHavingValue = notDisabledTarif.filter(function(){ 
      return $(this).val() !== ""; 
     }); 

     if (elementHavingValue.length) { 
      alert ("hello earth"); 
     } 
    } 
}); 
+0

bro。問題は、 '.tarif'が変更されるたびに無効または空の値があるかどうかをチェックする必要があります。現在の選択だけではありません。 '#kurir_list'の下にあるselect要素のすべて –

+0

@RobertHanson、説明のために、select要素に値があり、無効ではないことを確認する必要があります。 – Satpal

+0

はい、そうです –

関連する問題