How to get a table cell value using jQuery?

后端 未结 9 1021
终归单人心
终归单人心 2020-11-22 14:41

I am trying to work out how to get the value of table cell for each row using jQuery.

My table looks like this:

相关标签:
9条回答
  • 2020-11-22 15:14

    try this :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
        <title>Untitled</title>
    
    <script type="text/javascript"><!--
    
    function getVal(e) {
        var targ;
        if (!e) var e = window.event;
        if (e.target) targ = e.target;
        else if (e.srcElement) targ = e.srcElement;
        if (targ.nodeType == 3) // defeat Safari bug
            targ = targ.parentNode;
    
        alert(targ.innerHTML);
    }
    
    onload = function() {
        var t = document.getElementById("main").getElementsByTagName("td");
        for ( var i = 0; i < t.length; i++ )
            t[i].onclick = getVal;
    }
    
    </script>
    
    
    
    <body>
    
    <table id="main"><tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr><tr>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr><tr>
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
    </tr></table>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 15:15

    This works

    $(document).ready(function() {
        for (var row = 0; row < 3; row++) {
            for (var col = 0; col < 3; col++) {
                $("#tbl").children().children()[row].children[col].innerHTML = "H!";
            }
        }
    });
    
    0 讨论(0)
  • 2020-11-22 15:18

    If you can, it might be worth using a class attribute on the TD containing the customer ID so you can write:

    $('#mytable tr').each(function() {
        var customerId = $(this).find(".customerIDCell").html();    
     });
    

    Essentially this is the same as the other solutions (possibly because I copy-pasted), but has the advantage that you won't need to change the structure of your code if you move around the columns, or even put the customer ID into a <span>, provided you keep the class attribute with it.

    By the way, I think you could do it in one selector:

    $('#mytable .customerIDCell').each(function() {
      alert($(this).html());
    });
    

    If that makes things easier.

    0 讨论(0)
  • 2020-11-22 15:26

    a less-jquerish approach:

    $('#mytable tr').each(function() {
        if (!this.rowIndex) return; // skip first row
        var customerId = this.cells[0].innerHTML;
    });
    

    this can obviously be changed to work with not-the-first cells.

    0 讨论(0)
  • 2020-11-22 15:27

    A working example: http://jsfiddle.net/0sgLbynd/

    <table>
    <tr>
        <td>0</td>
        <td class="ms-vb2">1</td>
        <td class="ms-vb2">2</td>
        <td class="ms-vb2">3</td>
        <td class="ms-vb2">4</td>
        <td class="ms-vb2">5</td>
        <td class="ms-vb2">6</td>
    </tr>
    </table>
    
    
    $(document).ready(function () {
    //alert("sss");
    $("td").each(function () {
        //alert($(this).html());
        $(this).html("aaaaaaa");
    });
    });
    
    0 讨论(0)
  • 2020-11-22 15:33

    Try this,

    $(document).ready(function(){
    $(".items").delegate("tr.classname", "click", function(data){
                alert(data.target.innerHTML);//this will show the inner html
        alert($(this).find('td:eq(0)').html());//this will alert the value in the 1st column.
        });
    });
    
    0 讨论(0)
提交回复
热议问题