0

I have a question regarding the find method.

I want to be able to identify the cell that contains the input tag.

my html is like

<table>
   <tr>
      <td> celll </td>
      <td> celll </td>
      <td> celll </td>
   </tr>
   <tr>
      <td> celll </td>
      <td> <span><input type='text'></span> </td>
      <td> <span><input type='text'></span> </td>
   </tr>
</table>

My Jquery

$('table').find('td').each(function(){
          if($(this).find("input").length){
             console.log('found')
          }
      })

My codes can't seem to find it. Any tips? Thanks a lot!

4

4 回答 4

1

而不是使用.find()两次,只需使用一次并获取.closest()单元格:

$('table').find('input').each(function(){
    var parent = $(this).closest('td');
    //do something with parent
});

演示:http: //jsfiddle.net/dirtyd77/kdL97/

于 2013-08-23T21:10:20.260 回答
0

尝试

$("table td:has(input)").each(...)

http://api.jquery.com/has-selector/

小提琴http://jsfiddle.net/tarabyte/wM2Tg/1/

于 2013-08-23T21:11:55.263 回答
0
var cells = $('table').find('td');

$.each(cells, function(i){
  var that = $(this);
  var hasInput = that.find("input");
    if(hasInput.length > 0){
        console.log("td #" + i + " has an input sire.");
    }
});
于 2013-08-23T22:29:56.780 回答
0

只需使用:$('table input').parents('td');

JSFiddle:http: //jsfiddle.net/aPjwE/

于 2013-08-23T21:11:28.763 回答