如何使用 jQuery 唯一标识表(具有多行)的行中的列值
<head>
<script type="text/javascript">
$(document).ready(function() {
$(".viewLink").click(function()
{
var requisiteId = $('.viewLink').attr('value');
alert(requisiteId);
var dataString = 'id='+ requisiteId;
$.ajax({
type: "POST",
url: "checkRequestExe.php",
data: dataString,
cache: false,
success: function(data)
{
$("#individualRequisite").show();
$("#eachRequisite tr").remove();
if (data != false)
{
$("#eachRequisite").prepend(data);
$("#eachRequisite").hide().fadeIn('slow');
}
}
});
return false;
});
});
</script>
</head>
<body>
<div style="width:752px; height:330px; overflow:auto;">
<table class="record" cellspacing="0" cellpadding="5">
<tr style="background-color:#808080; font-size:12px; color:white; font-weight:bold;">
<td width="400">Title</td>
<td width="100">Requestor</td>
<td width="145">Date Request</td>
<td width="80">Status</td>
</tr>
<?
$count=0;
while ($data = mysql_fetch_array($results))
{
$requisiteId = $data["RequisiteId"];
$title = $data["Title"];
$userId = $data["UserId"];
$date = $data["DateRequest"];
$status = $data["Status"];
$color = ($count%2==0)?'CCCCFF':'E8E8E8';
?>
<tr style="background-color:#<?=$color?>; color:black; font-weight:bold;">
<td><?=$title?></a></td>
<td><?=$userId?></a></td>
<td><?=$date?></td>
<td>
<?
if ($status == 0)
{
$word = "Pending";
}
else
{
$word = "Completed";
}
echo "<a href='#individualRequisite' class='viewLink' value='$requisiteId'>$word</a>";
?>
</td>
</tr>
<?
$count++;
}
?>
</table>
</div>
</body>
我怎样才能唯一地标识类 viewLink ,以便每当单击该类的任何锚点时,锚点的值(“requiredId”)都会传递到 jquery 中,然后传递到另一个脚本中进行处理?
目前的问题是它会不断被最新的RequiredId替换。 有什么解决办法吗?
问题就在这里 [var RequiredId = $('.viewLink').attr('value');
] 即使我单击不同行的锚点,其中 requiredId
也将始终相同。
<head>
<script type="text/javascript">
$(document).ready(function() {
$(".viewLink").click(function()
{
var requisiteId = $('.viewLink').attr('value');
alert(requisiteId);
var dataString = 'id='+ requisiteId;
$.ajax({
type: "POST",
url: "checkRequestExe.php",
data: dataString,
cache: false,
success: function(data)
{
$("#individualRequisite").show();
$("#eachRequisite tr").remove();
if (data != false)
{
$("#eachRequisite").prepend(data);
$("#eachRequisite").hide().fadeIn('slow');
}
}
});
return false;
});
});
</script>
</head>
<body>
<div style="width:752px; height:330px; overflow:auto;">
<table class="record" cellspacing="0" cellpadding="5">
<tr style="background-color:#808080; font-size:12px; color:white; font-weight:bold;">
<td width="400">Title</td>
<td width="100">Requestor</td>
<td width="145">Date Request</td>
<td width="80">Status</td>
</tr>
<?
$count=0;
while ($data = mysql_fetch_array($results))
{
$requisiteId = $data["RequisiteId"];
$title = $data["Title"];
$userId = $data["UserId"];
$date = $data["DateRequest"];
$status = $data["Status"];
$color = ($count%2==0)?'CCCCFF':'E8E8E8';
?>
<tr style="background-color:#<?=$color?>; color:black; font-weight:bold;">
<td><?=$title?></a></td>
<td><?=$userId?></a></td>
<td><?=$date?></td>
<td>
<?
if ($status == 0)
{
$word = "Pending";
}
else
{
$word = "Completed";
}
echo "<a href='#individualRequisite' class='viewLink' value='$requisiteId'>$word</a>";
?>
</td>
</tr>
<?
$count++;
}
?>
</table>
</div>
</body>
How can I uniquely identify the class viewLink so whenever any anchor with that class is clicked, the value of the anchor("requisiteId")is passed into jquery and subsequently being passed into another script to handle?
The current problem is that it will continuously being replaced with the latest requisiteId.
Any solution?
The problem is here [var requisiteId = $('.viewLink').attr('value');
]
where the requisiteId
will always be the same even I clicked the anchor of a different row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
线
我认为而不是你想要的
I think instead of the line
you want
您的问题是有多个 viewLink 对象,因此当您运行 $('.viewLink') 时,您实际上会返回一个项目列表。
如果您在列表中调用 .attr(),您将得到第一个。您需要这样做:
这将为您返回目标项目的属性。
Your issue is that there are multiple viewLink objects, so when you run $('.viewLink'), you're actually getting back a list of items.
If you call .attr() on the list, you'll get the first one back. You need to do this:
Which will give you back the attr of the target item.