我想获得点击元素所在行的第一个<td>
的值。这是我使用的代码:
触发功能时,结果为“未定义”。我哪里错了?
function verifyOfflinepayment() {
$("#verifying").show();
alert($(this).closest('tr').find('td').eq(0).html());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>$paymentId</td>
<td>
<a href='#modal$i'>مشاهده رسید</a>
</td>
<td onclick='verifyOfflinepayment()'>
$persian_date
</td>
<td>
<div class='pretty p-icon p-round p-pulse'>
<input type='checkbox' />
<div class='state p-success'>
<i class='icon mdi mdi-check'></i>
<label>تایید</label>
</div>
</div>
</td>
</tr>
</table>
答案 0 :(得分:1)
试试这个
function verifyOfflinepayment(event) {
$("#verifying").show();
alert($(event).parent().find('td').eq(0).html());
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>$paymentId</td>
<td>
<a href='#modal$i'>مشاهده رسید</a>
</td>
<td onclick='verifyOfflinepayment(this)'>
$persian_date
</td>
<td>
<div class='pretty p-icon p-round p-pulse'>
<input type='checkbox' />
<div class='state p-success'>
<i class='icon mdi mdi-check'></i>
<label>تایید</label>
</div>
</div>
</td>
</tr>
</table>
&#13;
答案 1 :(得分:0)
您需要在verifyOfflinepayment
函数中传递所点击元素的引用,例如verifyOfflinepayment(this)
。目前$(this)
引用了您的窗口对象,因此您将获得undefined
。
function verifyOfflinepayment(elem) {
$("#verifying").show();
alert($(elem).closest('tr').find('td:eq(0)').html());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>$paymentId</td>
<td>
<a href='#modal$i'>مشاهده رسید</a>
</td>
<td onclick='verifyOfflinepayment(this)'>
$persian_date
</td>
<td>
<div class='pretty p-icon p-round p-pulse'>
<input type='checkbox' />
<div class='state p-success' >
<i class='icon mdi mdi-check' ></i>
<label>تایید</label>
</div>
</div>
</td>
</tr>
</table>