【发布时间】:2014-12-14 17:23:49
【问题描述】:
我正在使用 PHP、Javascript 和 MySQL。我的目标是在每一行都有一个按钮。该按钮应调用 onclick 功能和 href 到另一个页面。
我的代码有问题 --> 只有第一行的按钮是可点击的...
我假设每个按钮的按钮 ID 必须是唯一的。我有哪些选项可以使所有按钮都可点击?
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td><button id='greenbutton' class='small pill green'>$row[0]</button></td>";
echo "<td><img src='/iframe/$row[7]'> $row[5]</td>";
echo "<td>$row[4] min(s)</td>";
echo "<td><img src='/iframe/$row[6]'> $row[2]</td>";
if ($row[3] == 0) {
echo "<td>Unknown</td>";
}
else {echo "<td>$row[3]</td>";}
echo "</tr>\n";
// FOR ONCLICK
echo "<script type='text/javascript'>";
echo "document.getElementById('greenbutton').onclick = function () {";
echo "location.href = '/user';";
echo "};";
echo "</script>";
}
echo "</table></center>";
【问题讨论】:
-
委托监听
table的事件,检查处理函数中的e.target,如果是按钮,做点什么。
标签: javascript php forms button onclick