[英]How to get the clicked row's id value?
I generated a table by getting the values from database. 我通过从数据库获取值来生成表。 I set the primary key value to the row's id value, I have button in that row also. 我将主键值设置为该行的id值,在该行中也有一个按钮。 How to get the respective id value when I click the button. 单击按钮时如何获取相应的id值。
while($pack = mysql_fetch_array($get_pack))
{
echo"<tr id='<php echo $pack[id];?>'>";
echo'<td> <input name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack()"/></td>';
}
function edit_pack()
{
alert("package editing")
alert(this.id)
}
Try this for a pure JavaScript solution: 尝试以下操作以获取纯JavaScript解决方案:
function edit_pack() {
alert(this.parentNode.parentNode.id);
}
The first parentNode
will get the <td>
, the second will get the <tr>
. 第一个parentNode
将获得<td>
,第二个将获得<tr>
。 We then get the id
attribute. 然后,我们获得id
属性。
Alternatively the syntax is a little nicer if you're using jQuery: 另外,如果您使用的是jQuery,则语法会更好一些:
function edit_pack() {
alert($(this).closest('tr')[0].id;
}
尝试这个:
var trid = $(this).closest('tr').attr('id');
You can set it as the parameter of the js function. 您可以将其设置为js函数的参数。
echo'<td> <input name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack( ' .$pack['id']. ' )"/></td>';
Then in the js 然后在js
function edit_pack(id){
alert(id);
}
Just put the value in the JS call 只需将值放在JS调用中
PHP 的PHP
while($pack = mysql_fetch_array($get_pack))
{
echo '<tr id="'.$pack[id].'">';
echo '<td> <input name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack('.$pack[id].')"/></td>';
}
Javascript Java脚本
function edit_pack(id) {
alert(id);
}
Probably this is what you need 可能这就是您所需要的
while($pack = mysql_fetch_array($get_pack))
{
echo"<tr id='<php echo $pack[id];?>'>";
echo'<td> <input parentrow_id='<php echo $pack[id];?>' name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack(this.parentrow_id)"/></td>';
}
If you do not want to use jquery then do as follows: 如果您不想使用jquery,请执行以下操作:
while($pack = mysql_fetch_array($get_pack))
{
echo"<tr id='<php echo $pack[id];?>'>";
echo'<td> <input name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack('<?php echo $pack[id];?>')"/></td>';
}
function edit_pack(pack_id)
{
alert("package editing");
alert(pack_id);
}
try this : 尝试这个 :
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("[name=edit_pack]").click(function(){
alert($(this).parents("tr").attr('id'));
});
});
</script>
<?php
echo"<table name='test'>";
echo"<tr id='20'>";
echo'<td> <input name="edit_pack" type="button" value="Edit" onclick="javascript:edit_pack()"/></td>';
echo"</tr>";
echo"</table>";
?>
while($pack = mysql_fetch_array($get_pack)){
echo"<tr id='$pack[id]'>";
echo " <td> <input name='edit_pack' type='button' value='Edit' onclick='edit_pick($pack[id]);'/></td>";
echo "</tr>";
}
Your can pass argument JavaScript function 您可以传递参数JavaScript函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.