在此示例中,我在div .copy_song
中找不到该值
我尝试了closest
,find
,prevAll
,但没有尝试。
每次返回undefined
。
<table id="canzoni_player" class="display responsive dataTable no-footer" cellspacing="0" width="100%" role="grid" aria-describedby="canzoni_player_info" style="position: absolute; top: 0px; left: 0px; width: 100%;">
<tbody>
<tr role="row" class="odd">
<td class="sorting_1">40044</td>
<td>TITLE</td>
<td>TITLE</td>
<td>
<div class="copy_song btn btn-danger" rel="40044">Copia >><input type="hidden" value="40044" id="valore_codice">
</div>
</td>
<td>108</td>
<td>2016</td>
<td>2A</td>
<td>8</td>
<td>
<div class="click_play start">button
<input type="hidden" value="TEST">
</div>
</td>
</tr>
<tr role="row" class="odd">
<td class="sorting_1">40045</td>
<td>TITLE 2</td>
<td>TITLE 2</td>
<td>
<div class="copy_song btn btn-danger" rel="40044">Copia >><input type="hidden" value="40044" id="valore_codice">
</div>
</td>
<td>120</td>
<td>2018</td>
<td>3A</td>
<td>9</td>
<td>
<div class="click_play start">button
<input type="hidden" value="TEST2">
</div>
</td>
</tr>
</tbody>
</table>
$(document).ready(function() {
$('#canzoni_player tbody .click_play').click(function(){
var canzone=$(this).find('input[type="hidden"]').val();
var canzone_player=$(this).closest().find('.copy_song').attr('rel');
alert (canzone_player);
});
});
所以我只能在此表行的.copy_song
div中找到值,因为每一行都有不同的值,所以我必须使用引用(this
)
我想找到div .copy_song的'rel'值,在我的情况下,如果我单击按钮,我必须找到'40044',如果我单击其他按钮,我必须找到'40045'
答案 0 :(得分:1)
您需要首先使用parents()查找父tr
,然后找到.copy_song
元素
$(document).ready(function() {
$('#canzoni_player tbody .click_play').click(function() {
var canzone = $(this).find('input[type="hidden"]').val();
var codice_titolo_canzone = $(this).parents('tr').find('.copy_song').find('input[type="hidden"]').val();
alert(codice_titolo_canzone);
});
});
body {
padding: 5px;
}
label {
font-weight: bold;
}
input[type=text] {
width: 20em
}
p {
margin: 1em 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="canzoni_player" class="display responsive dataTable no-footer" cellspacing="0" width="100%" role="grid" aria-describedby="canzoni_player_info" style="position: absolute; top: 0px; left: 0px; width: 100%;">
<tbody>
<tr role="row" class="odd">
<td class="sorting_1">40056</td>
<td>TEXT</td>
<td>TEXT</td>
<td>
<div class="copy_song btn btn-danger" rel="40056">Copia >><input type="hidden" value="40056" id="valore_codice">
</div>
</td>
<td>64</td>
<td>2017</td>
<td>6A</td>
<td>7</td>
<td>
<div class="click_play start">button
<input type="hidden" value="test">
</div>
</td>
</tbody>
</table>
答案 1 :(得分:1)
将$(this).closest().find('.copy_song')
更改为
$(this).parents('tr').find('.copy_song')
首先选择父项tr
。