我的最终目标是单击表格上的一行,并将其填充到旁边的表单中以进行更新。在我对表单的填充进行编码之前,此处的代码只是“尝试”单击一行并将其与行数据一起显示在 Javascript 警报中。
这里我使用 PHP 填充了该表。 TR 类有“updatetbl”,稍后与 javascript 一起使用。我使用了这个问题的代码示例:Get the table row data with a click
if ($patients->num_rows > 0){
while($row = $patients-> fetch_assoc()) {
echo '<tr class="updatetbl">';
echo '<td class="updatetbl">' . $row['ID'] . '</td>';
echo '<td class="updatetbl">' . $row['Forename'] . '</td>';
echo '<td class="updatetbl">' . $row['Surname'] . '</td>';
echo '<td class="updatetbl">' . $row['DOB'] . '</td>';
echo '<td class="updatetbl">' . $row['Address'] . '</td>';
echo '<td class="updatetbl">' . $row['Postcode'] . '</td>';
echo '<td class="updatetbl">' . $row['Telephone'] . '</td>';
echo '<td class="updatetbl">' . $row['Email'] . '</td>';
echo '<tr>';
}
}else{
echo "0 results";
}
我试图首先使用 Javascript 将数据获取到警报中(我只是想在放入表单之前查看我是否拥有数据)
要填充警报并注册点击,我有以下 JavaScript。
<script>
$("tr.updatetbl").click(function(){
var tableData = $(this).children("td").map(function(){
return $(this).text();
}).get();
alert("Data = "+ $trim(tableData[0]) + " , " + $trim(tableData[1]) + " , "
+ $trim(tableData[2]) + " , " + $trim(tableData[3]) + " , "));
});
</script>
据我所知,tr.updatetbl 集中于单击此处?然后收集表数据..
有什么想法吗?
更新:我确实有等等
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Forename</th>
<th>Surname</th>
<th>D.O.B</th>
<th>Address</th>
<th>Postcode</th>
<th>Tel</th>
<th>Email</th>
</tr>
</thead>
<?php
$servername = removed
$dbname = removed
$username = removed
$password = removed
$connection = new mysqli($servername, $username, $password, $dbname);
if ($connection->connect_error) {
die('Could not connect: ' . $connection->connect_error);
}
$sql = "SELECT ID, Forename, Surname, DOB, Telephone, Email, Address, Postcode FROM people";
$people= $connection->query($sql);
if ($patients->num_rows > 0){
while($row = $patients-> fetch_assoc()) {
echo '<tr class="updatetbl">';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Forename'] . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo '<td>' . $row['DOB'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Telephone'] . '</td>';
echo '<td>' . $row['Email'] . '</td>';
echo '</tr>';
}
}
else
{
echo "0 results";
}
$connection->close();
?>
</table>
已解决:
我已经弄清楚了 - 我将编译后的代码放入带有 javascript 的新 jFiddle 中,但它仍然不起作用。我不明白为什么,所以我看了你的,我注意到你的库是 jQuery 的!我的是标准的 JS。所以我意识到我没有包含 <script src="jquery-1.11.3.min.js"></script>
我投票了下面的答案,因为我毫不费力地得到了帮助,尽管我确信所有其他答案都会起作用。
最佳答案
这一行末尾有一个“)”,这是不需要的,这就是它不起作用的原因:
`alert("Data = "+ $trim(tableData[0]) + " , " + $trim(tableData[1]) + " , "
+ $trim(tableData[2]) + " , " + $trim(tableData[3]) + " , ")); which isnt needed.
此外,只需在行上添加类 updatetbl
即可。
HTML
<table>
<tbody>
<tr class="updatetbl">
<td >ID</td>
<td >Forename</td>
<td >Surname</td>
<td >DOB</td>
<td >Address</td>
<td >Postcode</td>
<td >Telephone</td>
<td >Email</td>
</tr>
<tbody>
</table>
JS:
$(".updatetbl").click(function(){
var tableData = $(this).children("td").map(function(){
return $(this).text();
}).get();
alert("Data = "+ tableData[0] + " , " + tableData[1] + " , "
+ tableData[2] + " , " + tableData[3] + " , ");
});
关于javascript - 从表行获取 HTML 数据点击进入 javascript 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33690466/