jquery - 使用 closest() 向上导航表 DOM

标签 jquery mysql

我目前正在使用 closest() 向上导航 dom 并使用它来查找行元素 tr 但它似乎返回一个空的警报对话框,指示什么都没有被检索。我做错了什么?

JavaScript

$(document).ready(function () {
$('.manage').click(function(){
  // find the row we are in
  var $self = $( self );
  var $row = $self.closest( 'tr' );

  // read the id
  var $idCell = $row.find( '.id' );
  var caseId = $idCell.text();

  alert(caseId);

  });
});

HTML:

 <table border="0" class="sortable">
      <tr>
        <th>ID</th>
        <th>Functions</th>
      </tr>
      <?php do { ?>
        <tr>
          <td class="id"><?php echo $row_cases['id'].'-'.$_GET["progress"]; ?></td>
          <td><img src="components/manage.png" width="16" height="16" class="manage">
        </tr>
        <?php } while ($row_cases = mysql_fetch_assoc($cases)); ?>
    </table>

提前致谢!

最佳答案

你正在使用 self 而不是 this,JavaScript 中没有 self。这应该有效:

$(document).ready(function () {
     $('.manage').click(function(){
          // find the row we are in
          var $row = $( this ).closest( 'tr' );

          // read the id
          var $idCell = $row.find( '.id' );
          var caseId = $idCell.text();

          alert(caseId);
     });
});

关于jquery - 使用 closest() 向上导航表 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10218138/

相关文章:

javascript - 具有单独操作值的 Ajax 表单

jQuery - 删除数组中的类

php - 当 PDO 中的其他一切正常时,无法执行 PDO 语句(更新 MySQL 表)

MySQL表设计/架构,表太大

mysql - Rails 关联模型聚合的性能改进

jquery - 有人知道 jQuery UI 鼠标小部件吗?

jquery - 从选择选项更改字体颜色

javascript - jquery模拟select中所有选项的点击事件,从多个select选项中获取数据

php - 电子邮件代码和验证码激活后,将详细信息从 email_activation 数据库添加到成员数据库

php - 从 PHP codeigniter 模型返回的结果集中获取单个值