2016-07-08 6 views
2

jqueryを使用してデータベースからデータを取得していて、複数のdivのIDとして使用したいとします。私はデータベースからデータをフェッチすることができ、ループを使用してデータを反復することもできます。しかし、ループ内で.prop()を使用しようとすると、全く機能しません。コンソールに値を記録しようとすると、その値は空になります。以下は私のコードです。私を助けてください。ここで即時にIDを作成するときにjqueryでdivを表示/非表示にできない

$(document).ready(function() { 
$.get('/getInfringementTypes', function(data) 
{ 
    var parsed = JSON.parse(data); 
    //I am getting the following data back from db 
    // ["trademark", "copyright", "patent", "design", "other", "item-infringement"] 
    var valueArray = []; 
    $(parsed).each(function(index,value) 
    { 
     valueArray[index] = value; 
    }); 
    var i = 0; 

    for(i=0; i < valueArray.length; i++) 
    { 
     $('#'+ valueArray[i]).click(function() { 
      if($('#'+valueArray[i]).prop('checked',true)) { 
       $('#'+valueArray[i]+'List').show('5000'); 
      } 
     }); 
     console.log('#'+valueArray[i]+'List'); 
    } 
    console.log(valueArray); 
}); 
}); 

私のHTMLコードは次のとおりです。

<div class="row"> 
    <section> 
     <b>Verify Violation *</b> 
     <br> @foreach(getInfringementTypes() as $infringements_type) 
     <input type="radio" name="groupVerify" value="{{$infringements_type->infringement_type}}" id="{{Str::slug($infringements_type->infringement_type)}}" /> 
     <label for="{{Str::slug($infringements_type->infringement_type)}}"> 
      {{$infringements_type->infringement_type}} 
     </label> 
     @endforeach 
    </section> 
    <br> 
    <section id="subCode"> 
     <div id="trademarkList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Trademark') as $subcode) 
      <input type="radio" name="groupTrademark" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
     <div id="copyrightList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Copyright') as $subcode) 
      <input type="radio" name="groupCopyright" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
     <div id="patentList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Patent') as $subcode) 
      <input type="radio" name="sub_code" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
     <div id="designList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Design') as $subcode) 
      <input type="radio" name="sub_code" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
     <div id="otherList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Other') as $subcode) 
      <input type="radio" name="sub_code" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
     <div id="itemList"> 
      <b>Sub Code *</b> 
      <br> @foreach(getParentCodeFromDb('Item infringement') as $subcode) 
      <input type="radio" name="sub_code" value="{{$subcode}}" id="{{$subcode}}" /> 
      <label for="{{$subcode}}"> 
       {{$subcode}} 
      </label> 
      @endforeach 
     </div> 
    </section> 
    <br> @if($filter['status'] == 'suspect') 
    <section> 
     <b>Product *</b> 
     <br> 
     <input name="groupProduct" type="radio" id="test1" /> 
     <label for="test1">Handbags-Rocco</label> 
     <input name="groupProduct" type="radio" id="test2" /> 
     <label for="test2">Handbags-Brenda</label> 
     <input name="groupProduct" type="radio" id="test3" /> 
     <label for="test3">Handbags-Dracy</label> 
     <input name="groupProduct" type="radio" id="test4" /> 
     <label for="test4">Handbags-Donna Hobo</label> 
     <input name="groupProduct" type="radio" id="test5" /> 
     <label for="test5">Handbags-Kristen</label> 
     <input name="groupProduct" type="radio" id="test6" /> 
     <label for="test6">Apparel-Apparel</label> 
     <input name="groupProduct" type="radio" id="test7" /> 
     <label for="test7">Footwear-Footwear</label> 
     <input name="groupProduct" type="radio" id="test8" /> 
     <label for="test8">Other-Other</label> 
    </section> 
    @endif 
</div> 
+0

あなたの問題が表示されますか? '$( '#' + valueArray [i])。prop( 'checked'、true)' if条件でifset関数を使います。それはここで働くことができない - それはいつも真実を返す! @ madalin ivascu – pleinx

答えて

2

は、次のことを試してください。

$.each(parsed,function(i,v){ 
    if($('#'+v).is(':checked')) { 
     $('#'+v+'List').show('5000'); 
     $('div[id$=List]').not($('#'+v+'List')).hide(); 
    } 
}); 
+0

からの回答を確認することができませんでした。あなたは私の質問をもっと詳しく説明したいですか? –

+0

解析されたデータが配列の場合 – madalinivascu

+0

これはコンソールにエラーがありますか? – madalinivascu

0

あなたが動的にイベント関数を生成している場合、あなたはメソッドの代わりに、入札にを使用することができますダイレクトセレクタのクリック

関連する問題