2012-11-22 28 views
0

我需要一種只調用一行文本的算法。jQuery在表格中找到文本

我的td包含數字和文本,我只想在我的「td」文本。

演示:http://jsfiddle.net/3yYFY/1

例如:

在第二行 = accure,鱷魚,獾

在第三行 =斑馬,大象

有很多行(td)我的表和文字不位於一個特定的列

HTML:

<table id="docsTable"> 
    <tr bgcolor="#E7DDCC" class="liste-0"> 
     <td align="center"><input type="checkbox"/></td> 
     <td>Document Title 1</td> 
     <td>567</td> 
    </tr> 
    <tr bgcolor="#E7DDCC"> 
     <td align="center"><input type="checkbox"/></td> 
     <td>accure</td> 
     <td>134</td> 
     <td>alligator</td> 
     <td>badger</td> 
    </tr> 
    <tr bgcolor="#E7DDCC"> 
     <td align="center"><input type="checkbox"/></td> 
     <td>Zebra</td> 
     <td>231</td> 
     <td>Elephant</td> 
    </tr> 
</table> 
<input type='button' id='btnTest' value='Get Rows' /> 

的jQuery:

$(function() { 
    $('#btnTest').click(function() { 
     var arr = $('#docsTable tr').has('input[type="checkbox"]:checked').map(function() { 
      alert($('td:eq(1)', this).text()) 
     }); 

    }); 
});​ 
+0

alert($(this).text())將返回每行的文本。這是你在找什麼? –

+0

我只需要連續的所有字母, 爲第二行中的一個例子:accure,鱷魚,獾 – aldimeola1122

回答

2

最終UPDATE

使用此:http://jsfiddle.net/uJdj9/2/

$(function() { 
    $('#btnTest').click(function() { 
     var arr = $('#docsTable tr').has('input[type="checkbox"]:checked').map(function() { 
      var text = ""; 
      $(this).find('td').each(function(){ 
       var txt = $(this).text(); 
       if(txt && !txt.match(/\d/g)) 
        text += txt + ', '; 
      }) 
      text = text.replace(/,([^,]*)$/,''); 
      alert(text); 
     }); 

    }); 
});​ 

或類似的,如果你知道至極TD丟棄:

DEMO:http://jsfiddle.net/uJdj9/

$(function() { 
    $('#btnTest').click(function() { 
     var arr = $('#docsTable tr').has('input[type="checkbox"]:checked').map(function() { 
      var text = ""; 
      $('td',this).not(':eq(2)').each(function(){ 
       text += $(this).text()?$(this).text() +', ':''; 
      }) 
      alert(text); 
     }); 

    }); 
});​ 
+0

謝謝,你可以看看這個演示,在第四行,沒有作品: http://jsfiddle.net/uJdj9/1/ 我有很多專欄,和字和數字在不同的列 – aldimeola1122

+0

查看更新的答案 –

+0

謝謝烤,它的作品完美。那正是我所尋找的。 – aldimeola1122

1

試試這個代碼

$(function() { 
$('#btnTest').click(function() { 
    var arr = $('#docsTable tr').has('input[type="checkbox"]:checked').map(function() { 
     alert($('td', this).text()) 
    }); 

}); 

});

http://jsfiddle.net/3yYFY/4/

+0

這是工作的感謝,但我想打電話只有字母不是數字, – aldimeola1122

+0

使用替換功能,以刪除數字 – Kathiravan