简体   繁体   English

取得tr jquery的td的每个值的动态值

[英]Get dynamic value every value of td of tr jquery

$("#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);
}

I have a code here that populates a table with student name.It dynamically gets it data from ajax request from database. 我这里有一个用学生姓名填充表格的代码,它动态地从数据库的ajax请求中获取数据。 I want to know how to get each value of the td just by clicking the class school_id . 我想知道如何通过单击class school_id来获取td每个值。 I want to get each value and be able to put it in input text box. 我想获取每个值并将其放入输入文本框中。 The end result will be like this after clickng the id 单击ID后,最终结果将如下所示

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

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

Since you are adding the table dynamically you 'll need something like this 由于您是动态添加表,因此需要这样的内容

$('#student').on('click', '. school_id', function(e) {
    var parent = $(e.target).closest('tr'); //gets the parent tr element
    var id = parent.find('#id').html(); //finds element inside specific parent that has an id of id
    var lastName = parent.find('#lastname').html(); //finds element inside specific parent that has an id of last name
    var firstName = parent.find('#firstname').html(); //finds element inside specific parent that has an id of first name
});
$(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);
    });

This will do what you want 这会做你想要的

jQuery('.school_id').click(function (event){
    var theTd = jQuery(this).parent();
    var theId = theTd.attr('id');
});

Something like this? 像这样吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM