Jquery“最近”和“查找”函数返回“未定义”!

时间:2018-01-01 13:42:18

标签: javascript jquery html

我想获得点击元素所在行的第一个<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>

2 个答案:

答案 0 :(得分:1)

试试这个

&#13;
&#13;
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;
&#13;
&#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>