我有一张表,每行都有链接。我正在尝试使用AJAX通过PHP从数据库中检索数据数组,并返回将在另一个DIV中打印的数据。
我正在尝试获取分配给每一行的属性值(链接ID)并将此值传递给查询。我无法获取链接ID,当我测试发出警报时,它不会显示。我看过几篇帖子并试了几样但没有好处。警报弹出窗口显示“未定义”。
<tr id="mashed_row">
<td class="linked-title dark unpadded">
<a href="#" id="linksplode" link_id="<?php echo $link['linkid']; ?>"><?php echo $link['keywords']; ?></a>
</td></tr>
<script type="text/javascript">
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).closest('tr').find('td').attr('link_id'));
});
});
</script>
答案 0 :(得分:1)
只需使用$(this).attr('link_id')
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).attr('link_id'));
});
});
答案 1 :(得分:1)
您的道明没有link_id
属性,您的<a>
拥有该属性。我创建了一个显示两个版本的示例:
$(document).ready(function(){
$('#mashed_row a').click(function () {
console.log("TD:", $(this).closest('tr').find('td').attr('link_id'));
console.log("A:", $(this).closest('tr').find('a').attr('link_id'));
// or if you really want to get the attribute from the clicked item, it's even easier:
console.log("direct:", $(this).attr('link_id'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
<tr id="mashed_row">
<td class="linked-title dark unpadded" link_id="link_id_TD">
<a href="#" id="linksplode" link_id="link_id_A">Hello</a>
</td>
</tr>
</table>
答案 2 :(得分:1)
你正试图 td attr not there ,试试这个,希望有所帮助: -
$(document).ready(function(){
$('#mashed_row a').click(function () {
alert($(this).attr('link_id'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
<tr id="mashed_row">
<td class="linked-title dark unpadded">
<a href="javascript:;" id="linksplode" link_id="12">key words</a>
</td>
</tr>
<table>