点击一个行除了首个TD(Clicking a row except for the first td

2019-09-18 07:15发布

我想点击一个表行和执行的操作。 但是,如果他们点击第一个<td>单元格我不希望执行的操作。

这里是我到目前为止的代码:

jQuery('#dyntable tbody tr').live('click', function () {
        nTr = jQuery(this)[0];

        openMessage(oTable, nTr);
    });

这如预期运作,除了第一个<td>都有一个复选框,这样,如果他们在该单元格单击我不想叫邻penMessage(oTable, nTr); 功能。

我也仍然需要nTr该行的=的内容。

Answer 1:

行内使用点击的目标,并检查TD指数

简化的演示: http://jsfiddle.net/WLR9E/

jQuery('#dyntable tbody tr').live('click', function (evt) {
    var $cell=$(evt.target).closest('td');
    if( $cell.index()>0){
       openMessage(oTable, this);
}
});

住()已过时,如果使用jQuery> = 1.7转换为开()。 下面假定主表是在页面永久的财富。

jQuery('#dyntable').on('click','tbody tr', function (evt) {
    var $cell=$(evt.target).closest('td');
    if( $cell.index()>0){
       openMessage(oTable, this);
   }
});

这条线在你的代码是redindant,它只是简单地返回同this

nTr = jQuery(this)[0];


Answer 2:

 $("#dyntable tbody").on('click', 'tr td:not(:first-child)',function () {
    alert("success");
});

 $(document).ready(function () { $("#tbluserdata tbody").on('click', 'tr td:not(:first-child)', function () { alert("write code"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <head> </head> <body> <table border="2" width="50%" id="tbluserdata"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Address</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>aaa</td> <td>ccc</td> <td>delete</td> </tr> <tr> <td>1</td> <td>aaa</td> <td>ccc</td> <td>delete</td> </tr> <tr> <td>1</td> <td>aaa</td> <td>ccc</td> <td>delete</td> </tr> </tbody> </table> </body> </html> 



Answer 3:

这将这样的伎俩:

jQuery('#dyntable tbody tr td:not(:first)').live('click', function () {
        nTr = jQuery(this).parent('tr')[0];

        openMessage("asdf", nTr);
});


function openMessage(oTable, nTr){
  console.log(nTr);
  alert('something happened');    
}

下面就来测试小提琴: DEMO



Answer 4:

你可以尝试添加类或唯一的ID是<td>元素。 然后在你的处理器,你对照它。

jQuery('#dyntable tbody tr').live('click', function () {
  var $thisRow = jQuery(this); // cache this selector for performance
  nTr = $thisRow[0];
  if (!$thisRow.hasClass('not_this_one')){
    openMessage(oTable, nTr);
  }
});


文章来源: Clicking a row except for the first td