2013-01-16 87 views
4

我想要像這樣使用jquery來獲取td的值。如何使用jQuery獲取TD值?

$("#id").find(".class").attr('value'); 
//or 
$("#id").find(".class").val(); 

兩者都返回空字符串,雖然它有一個值。 請注意*我正在嘗試獲取動態創建的元素的值。 在此先感謝。

+0

分享您的HTML結構 –

+0

檢查這篇文章如何讓表格單元格TD值的http:// codepedia。 info/jquery-get-table-cell-td-value-div/ –

回答

4

val()函數主要用於獲取表單元素的值,如input,select和textarea。您需要text()html()函數來獲取td的內容。

要獲得文本

textOfTd = $("#id").find(".class").text(); 

將HTML

textOfTd = $("#id").find(".class").html(); 
+0

現在很感謝它。 –

+0

歡迎您@EjazKarim,http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Adil

10

只要寫

$("#id .class").text(); 

或獲得的HTML使用,

$("#id .class").html(); 
2

HTML:

<table> 
    <tr> 
     <td class="tdcls">1</td> 
     <td class="tdcls">2</td> 
     <td class="tdcls">3</td> 
    </tr> 
    <tr> 
     <td class="tdcls">4</td> 
     <td class="tdcls">5</td> 
     <td class="tdcls">6</td> 
    </tr> 
</table> 

jQuery代碼來選擇特定的td值:

$(".tdcls").mouseenter(function(){ 
    var a = $(this).text(); 
});