2014-10-10 175 views
0

我该如何去查找一个值为(foo)的值(文本)并将该行中的下一个单元格设置为“bar”的表格单元格?jquery搜索表格单元格 - >更新相邻单元格

要设置可能已经包含了需要被重写一些文本的单元格。

+1

你有什么?请显示一些代码 – 999k 2014-10-10 15:13:26

回答

1

试试这个,但我haven`t进行了测试。

$('td').filter(function(){ 
    return $(this).text() === 'foo' 
}).next('td').text('bar') 

Demo

0
var rowList = document.getElementById('tableID').children; 
for (var i=0, len = rowList.length; i < len; ++i) { 
    var row = rowList[i]; 
    var cellList = row.children; 
    for (var j=0, size = cellList.length; j < size; ++j) { 
     var cell = cellList[j]; 
     //need additional check to avoid out of bounds issues 
     if (cell.innerHTML == 'foo' && cellList[j + 1]) { 
      cellList[j + 1].innerHTML = 'bar'; 
     } 
    } 
} 

编辑

遗憾错过了jQuery的标签。上面的答案是好的,但我相信拨打.text()应该是.html()