2017-09-27 64 views
0

我有我用foreach分發的數據列表。我想用bootstrap模態獲得每個數據的細節。如何獲得laravel上的Bootstrap模式數據5

這是我的代碼

<div class="container bg-white" style="padding: 20px;"> 
     @foreach ($hadiah as $h) 
     <div class="col-md-6 rewards" > 
      <div class="rewards-box"> 
       <div class="img"> 
        <img class="imagePreview" src="https://admin.philipscontractorclub.com/uploads/rewards/{{$h->image}}"> 
       </div> 
       <div class="caption"> 
        <h3 class="title">{{$h->title}}</h3> 
        <p class="point_needed"><span class="fa fa-certificate"></span> {{$h->point_needed}}</p> 
        <p class="description">{{$h->description}}</p> 
        <button type="button" class="btn btn-info btn-lg detail-item" data-id="{{ $h->id }}" data-toggle="modal" data-target="#myModal">Buka</button> 
       </div> 
      </div> 
     </div> 
     @endforeach   
    </div> 

這是我的模態

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 

    <!-- Modal content--> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title"></h4> 
     </div> 
     <div class="modal-body"> 
     <div class='row'> 
      <center> 
       <div class='col-md-12'> 
        <img class="modal-image"> 
       </div> 
      </center> 
      <div class='col-md-12'> 
       <p align='center' class="modal-poin"><b>Poin dibutuhkan</b></p> 
      </div> 
      <div class='col-md-12'> 
       <p align='justify' class="modal-desc">Deskripsi</p> 
      </div> 
     </div> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 

    </div> 
</div> 

這是我的javascript

<script type="text/javascript"> 
    $("body").on("click",".detail-item",function(){ 
     var id = $(this).data('id'); 
     var nama = $(this).find('title').text(); 
     var src = $(this).find('.imagePreview').attr('src'); 
     var poin = $(this).find('point_needed').text(); 
     var desc = $(this).find('description').text(); 
     $("#myModal").find(".modal-title").text(nama); 
     $("#myModal").find('.modal-image').attr('src', src); 
     $("#myModal").find(".modal-poin").text(poin); 
     $("#myModal").find(".modal-desc").text(desc); 
    }); 
</script> 

結果是零中。我想每一個數據的細節,如果我按一下按鈕

感謝您的幫助

回答

1

你需要使用jQuery中的.parent()方法來獲得你想要的結果。這是因爲當你說$(this)時,你只是指被點擊的按鈕,而不是整個按鈕是小孩的.caption div。

請嘗試執行此代碼。

$("body").on("click",".detail-item",function(){ 
    var id = $(this).data('id'); 
    var nama = $(this).parent().find('.title').text(); 
    var src = $(this).parent().parent().find('.imagePreview').attr('src'); 
    var poin = $(this).parent().find('.point_needed').text(); 
    var desc = $(this).parent().find('.description').text(); 

    // rest of you code here ... 
}); 

注意的是,在.imagePreview的情況下,我叫.parent()方法兩次,因爲它實際上是「爺爺」按鈕的元素。

Please refer to the docs for more references.

+0

謝謝,那個工作。我忘了輸入'。'在find('title')。text() –