0
我正在使用循環從我的數據庫中獲取數據列表。我循環一個div,但值取決於我在數據庫中的變化。我會想要獲得我從各個div上點擊的鏈接的id。下面是我的html代碼...在jquery上單擊時獲取循環元素的標識
<?php
$query = $student->conn->prepare('SELECT * FROM groups');
$query->execute();
$result = $query -> fetchAll();
$count = 0;
foreach ($result as $row) {
$count = $count + 1;
$groupName = $row['GpName'];
$groupAdmin = $row['GpAdmin'];
$groupPurpose = $row['GpPurpose'];
$output = "42";
echo "<div class='animated flipInY col-sm-4'>
<div class='tile-stats'>
<div class='icon'><i class='fa fa-tags'></i></div>
<div class='count'>$count</div>
<h3 id='groupName'>$groupName</h3>
<a id='$groupName' class='display' href='#'><p>Click here display group information.</p></a>
</div>
</div>";
}
?>
這是我使用的jQuery代碼。它工作在控制檯,但它並沒有在網頁上運行:
$('.display').on('click', function() {
$(this).next();
var id = $(this).prev().val();
alert(id);
});
看看[this](http://stackoverflow.com/a/3239600/1272394)。 – Drewness
現在你正在創建重複的id('id ='groupName''),這是行不通的。 ID必須是唯一的。 '.display'後面沒有'next()'(兄弟)。你可以擺脫那行代碼。通常'var id = $(this).prev('h3')。attr('id');'[退出使用alert()進行故障排除。](http://stravid.com/en/stop- -javascript-alert-madness /),請改用'console.log()'。 –
發佈呈現的HTML,而不是PHP。 – j08691