开发者

To get a value of a TD using JQuery

I got a very simple Table with only two rows.

I was thinking what is the best way to get the value from the TD with ID "row2".

<Table id="testing>
<tr>
<th>
</th>
<td id="row1">hello</td&开发者_C百科gt;
</tr>
<tr>
<th>
</th>
<td id="row2">world</td>
</tr>
</table>

Here is my attempt:

$(document).ready(function(){ 
      var r=$("#testing":row2).val();
      alert(r);
});

But I couldn't see any message pop up. What shall I do in the JQuery code if I want to specify the Table ID along with the TD ID?

 var r=$("#testing":row2).text();
 var r=$("#testing").children("row2").text();


This will do it for you:

  var r = $("#testing #row2").text();
  alert(r);

In action here for your viewing pleasure.


Use text() instead of val()

var r = $("#row2").text();

More Info:

  • .text()


    <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>                   

jquery code to select particular td value

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


the TD ID is going to be unique be it in any table. It is not right to have two tables with TD ID's same in both tables. Therefore if you feel then append the table id for the TD ID like so: (and then use the answers above)

 <table id="test1">
    <tr>
    <th>
    </th>
    <td id="test1_row1">hello</td>
    </tr>
    <tr>
    <th>
    </th>
    <td id="test1_row2">world</td>
    </tr>
 </table>

does this help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜