2016-07-08 45 views
2

我正在使用jquery從數據庫中獲取數據,並希望將其用作多個div的ID。我能夠從數據庫獲取數據,然後能夠使用循環迭代數據。但是當我嘗試在循環內使用.prop()時,它根本不起作用。當我嘗試在控制檯上記錄這些值時,它是空的。以下是我的代碼。請幫助我。無法使用jquery顯示/隱藏div時,在飛行中創建ID

$(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條件中使用.setter函數。這不能在這裏工作 - 它總是返回真實的!從@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

,如果你是動態生成事件的功能,你可以在方法使用,而不是招標直接點擊選擇器

$('#'+ valueArray[i]).on("click", function() { 
    console.log($(this).text()); 
});