To get a value of a TD using JQuery

前端 未结 4 602
予麋鹿
予麋鹿 2021-01-12 05:32

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\".

相关标签:
4条回答
  • 2021-01-12 05:56

    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 讨论(0)
  • 2021-01-12 06:09

    Use text() instead of val()

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

    More Info:

    • .text()
    0 讨论(0)
  • 2021-01-12 06:09
        <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();
    });
    
    0 讨论(0)
  • 2021-01-12 06:11

    This will do it for you:

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

    In action here for your viewing pleasure.

    0 讨论(0)
提交回复
热议问题