2016-11-09 64 views
2

下面是我的PHP代碼。我顯示3張圖片,如果我點擊任何圖片或產品,我需要在彈出窗口中添加該產品。我試圖獲得產品名稱,但我不能。如何解決這個問題。無法獲取href使用jQuery的屬性

<?php 
    $i = 0; 
    while ($row = mysql_fetch_assoc($result)) 
    { 
     echo"<td style='background-color:#FFFFFF;' height='383'> 
       <a href='#myModal?productname={$row["productname"]} 'id='burl' role='button' data-productname='{$row["productname"]}' data-toggle='modal' data-target='#myModal'> 
        <img src='images/{$row["productname"]}.jpg' width='255'/> 
       </a> 
      </td>"; 
    } 
    mysql_free_result($result); 
?> 

JQuery的值編碼:

$(document).ready(function() { 
    $('#myModal').on('shown.bs.modal', function() { 
     console.log($(this).data('productname')); 
    }) 
}) 
+1

通常只是$(選擇).attr( 'href' 屬性); –

+1

請勿使用* mysqli_fetch_assos *而不使用* mysql_fetch_assoc * –

+0

'$(this).attr('data-productname')' – Beginner

回答

0
$(document).ready(function() 
{ 
      $('#myPopup').on('show.bs.modal', function (e) 
{ 
       var rowid = $(e.relatedTarget).data('productname'); 
       alert(rowid); 
       $.ajax(
{ 
        type: 'GET', 
        url: 'fetch_data.php', //Here you will fetch records 
        data: 'productname=' + rowid, //Pass $id 
        success: function (data) 
{ 
         $('.fetched-data').html(data);//Show fetched data from database      
        } 
       }); 
      }); 
     }); 

This coding solved my problem. 
Thanks for all your reply. 
0

$(this)模態事件中是不是你點擊的元素實例,但模式本身。您應該使用relatedTarget像這樣:

$(document).ready(function() { 
    $('#myModal').on('shown.bs.modal', function (e) { 
              // ^--- Add here 
     var 
     relatedData = $(e.relatedTarget), 
     productname = relatedData.data('productname'); 

     console.log(productname); 
    }) 
})