从使用 MySql 创建的表中获取 ROW ID 以与 OnClick 函数一起使用
我有一个使用 PHP 从 Sql 数据库创建的表,该表成功显示了表信息 使用以下代码示例。
<table class="mytable loottable" id="guildmembers">
<?php
$query = mysql_query("SELECT $member.charname, $member.char_lvl, $trskill.skill_desc, .....
if (mysql_num_rows($query) > 0) {
while (false !== ($row = mysql_fetch_assoc($query))) {
echo '<tr id="'.$row['charname'].'">'; <-- setting row id here
echo '<td class="lootrow" id="memth1">'.$row['charname'].'</td>';
echo '<td class="lootrow" id="memth2">'.$row['rank_desc'].'</td>';
echo '<td class="lootrow" id="memth3">'.$row['class_name'].'</td>';
echo '<td class="lootrow" id="memth4">'.$row['char_lvl'].'</td>';
echo"</tr>\n";
}
mysql_free_result($query);
}
?>
</table>
页面源示例,我已验证每个表行都分配了正确的 ID。
<tr id="Calysta"><td class="lootrow" id="memth1">Calysta</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Inquisitor</td></tr>
<tr id="Rynanx"><td class="lootrow" id="memth1">Rynanx</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Mystic</td>
我将每个表 ROW ID 设置为始终唯一的第一个字段。然后我尝试用这个 以下函数中的 ID 用于在用户单击表中的行时将用户重定向到个人资料页面。
<script type="text/javascript">
$(document).ready(function () {
$("table.mytable").each( function() {
table = $(this);
if ($(table).attr("id")=="guildmembers") {
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + $(this).attr("id") + "/";
});
}
})
});
</script>
应该在浏览器中打开一个具有以下地址的新页面,例如如果 单击第一行:“http://everquest2.com/Kithicor/Calysta/”,
无论单击哪一行,都会创建正在创建的 Web url 是“http://everquest2.com/Kithicor/guildmembers/”,
这是表 ID。不是被单击的行的 ID。我尝试过各种解决方案 并且无法弄清楚如何在此单击函数中获取行 ID。对此有任何帮助 非常感谢。谢谢。
I have a table created with PHP from an Sql Database that successfully displays the table information
using the following code example.
<table class="mytable loottable" id="guildmembers">
<?php
$query = mysql_query("SELECT $member.charname, $member.char_lvl, $trskill.skill_desc, .....
if (mysql_num_rows($query) > 0) {
while (false !== ($row = mysql_fetch_assoc($query))) {
echo '<tr id="'.$row['charname'].'">'; <-- setting row id here
echo '<td class="lootrow" id="memth1">'.$row['charname'].'</td>';
echo '<td class="lootrow" id="memth2">'.$row['rank_desc'].'</td>';
echo '<td class="lootrow" id="memth3">'.$row['class_name'].'</td>';
echo '<td class="lootrow" id="memth4">'.$row['char_lvl'].'</td>';
echo"</tr>\n";
}
mysql_free_result($query);
}
?>
</table>
Example of page source where i have verified that each table row is assigned the correct id.
<tr id="Calysta"><td class="lootrow" id="memth1">Calysta</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Inquisitor</td></tr>
<tr id="Rynanx"><td class="lootrow" id="memth1">Rynanx</td><td class="lootrow" id="memth2">Guild Leader</td><td class="lootrow" id="memth3">Mystic</td>
I am setting each table ROW ID to the first field which is always unique. Then i am trying to use this
ID in the following function to redirect users to a profile page when they click on a row in the table.
<script type="text/javascript">
$(document).ready(function () {
$("table.mytable").each( function() {
table = $(this);
if ($(table).attr("id")=="guildmembers") {
$(this).click(function(){
window.location.href = "http://eq2players.com"+
"/Kithicor/" + $(this).attr("id") + "/";
});
}
})
});
</script>
Which should open a new page in the browser with the following address for instance if the
first row is clicked : "http://everquest2.com/Kithicor/Calysta/"
Instead no matter which row is clicked the web url being created
is "http://everquest2.com/Kithicor/guildmembers/"
which is the Tables ID. not the ID of the row being clicked. I have tried various solutions
and cannot figure how to get the row ID in this click function. Any help with this would
much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开始吧,只是快速完成此操作,更改了一些内容,请注意使用重复的 id 是不好的,你不应该这样做。而且你的Javascript过于复杂:
HTML示例:
jQuery:
jsFiddle链接:
http://jsfiddle.net/TJBX5/
Here you go, just did this quick, changed some things, please be mindful of using duplicate id's its BAD, you shouldn't. Also you over complicated your Javascript:
HTML EXAMPLE:
jQuery:
jsFiddle Link:
http://jsfiddle.net/TJBX5/
您正在桌子上安装点击处理程序。因此,当您调用
$(this).attr("id")
时,您正在询问表的 id。您需要在表中的每一行安装点击处理程序。You are installing your click handler on the table. Therefore, when you call
$(this).attr("id")
, you are asking for the table's id. You need to install the click handler on each row in the table.