我遇到問題,當我點擊表格時,沒有顯示任何內容。 在表格行點擊,提示框顯示點擊值
當我點擊讓我們說客戶名='約翰'的行,名字'約翰'應該出現在警告框上,但沒有任何反應。
這是我的表單擊代碼。
$(document).ready(function(){
$("#parentElementIdHere").on("click", "#test tr", function(e) {
var name = $(this).find("td").first().text();
alert(name);
});
而且,這裏是我生成的表代碼。
if(!empty($_GET['q'])){
$q = $_GET['q'];
$query="select * from customer where customername like '$q%'";
$result = mysqli_query($dbconn,$query);
echo "<div id='parentElementIdHere'>";
echo "<table id='test' border=3>
<tr>
<th>Customername</th>
<th>nric</th>
<th>email</th>
<th>mobileno</th>
<th>telephoneno</th>
<th>address</th>
<th>postalcode</th>
<th>datejoined</th>
<th>points</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['nric'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['mobileno'] . "</td>";
echo "<td>" . $row['telephoneno'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['postalcode'] . "</td>";
echo "<td>" . $row['datejoined'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
難道問題是因爲我的表生成是由於ajax不斷變化?
是的,通過ajax動態更改表是* one *問題。另一個問題是你的選擇器'#test tr#name''說選擇帶有id =「name」的元素,它們是'tr'元素的後代*,但它實際上就是你所有的'tr'元素有這個ID。更不用說重複的ID是無效的HTML。 – nnnnnn