$("#student").find("tr:gt(0)").remove();
for (var i = 0; i < data.length; i++) {
    tr = $('<tr/>');
    tr.append("<td id='id'>" + "<a class=\"school_id\"  id=" + data[i].id + " href='#' value='" + data[i].id + "'>" + data[i].id + "</a>" + "</td>");
    tr.append("<td id='lastname'>" + data[i].lastname + "</td>");
    tr.append("<td id='firstname'>" + data[i].firstname + "</td>");
    //$("#td_id").attr('width', '15%');
    //$('tr td:nth-child(1)').get(0).style.width = '100px';
    //tr.find('td:nth-child(2)').width('30%');‌​
    $("#student").append(tr);
}


我这里有一个用学生姓名填充表格的代码,它动态地从数据库的ajax请求中获取数据。我想知道如何通过单击类td来获取school_id的每个值。我想获取每个值并将其放入输入文本框中。单击ID后,最终结果将如下所示

var id = $('#tdwithidid').text();
var lastname = $('#tdwithidlastname').text();
var firstname = $('#tdwithidfirstname').text();

$('#id').val(id);
$('#lastname').val(lastname);
$('#firstname').val(firstname);

最佳答案

$(document).on('click', '#tableid tr', function(e) {
        var id = $(this).find('td:nth-child(1)').text();
        var lastname = $(this).find('td:nth-child(2)').text();
        var firstname = $(this).find('td:nth-child(3)').text();

        $('#id').val(id);
        $('#lastname').val(lastname);
        $('#firstname').val(firstname);
        console.log(tin + lastname + firstname);
    });


这会做你想要的

09-16 18:19