开发者

How do I get a specific <TD> value?

开发者 https://www.devze.com 2023-03-09 09:44 出处:网络
I\'m facing a troubles around j开发者_运维技巧Query and HTML elements. Let\'s to the \"Artists\":

I'm facing a troubles around j开发者_运维技巧Query and HTML elements. Let's to the "Artists":

<table id="tableBenef" width="375px">
     <tr style="border-bottom: solid 1px silver;">
         <td style="width: 10%"> Action </td>
         <td style="width: 45%"> Nome   </td>
         <td style="width: 20%"> Data Nasc </td>
         <td style="width: 25%"> Grau Parent </td>
     </tr>
</table>

This above is the Table Header and Inside of it I add dinamically new Rows. Each row contains a Checkbox and three more TDs, also I have three text inputs which I send each of one to the referent TD, the checkbox is for Delete purposes.

The question is, How to Get those new values inserted dinamically with jQuery?! I started with this code below:

 function getRowValues() {
            var table = $("#tableBenef");
            $(table).each(function () {
                alert($(this).text());
            })
        };

But I'm facing trouble with that, could someone help me on this?

In negative case, thanks anyway! Sorry for any mistake.


Cheap solution: don't get them back from the HTML at all. Instead, before inserting each row into the HTML, store that row's values in an array. Then, just read the array rather than having to go all the way back through the DOM.

As requested, here is some very basic code for how you'd go about doing this. (Obviously, this is just code to illustrate the general idea, since I don't know your actual setup.)

var insertedRowData = [];

function insertRow(rowValues) {
  // I'm assuming that rowValues is in the form {key1: "value1", key2: "value2"}.
  // That sort of generic object is probably a good format for storage,
  // but it doesn't matter what format you use, so long as it is a single object
  // that can be stored now and accessed later.

  insertedRowData[insertedRowData.length] = rowValues;

  /*
    ...
    Insert the row into the table, like you're already doing.
    ...
  */
}

function handleRowValues() {
  // Hey, look! All of the data is still stored in insertedRowData.
  // I can now handle it in whatever way I want. I think I'll log them
  // to Chrome or Firebug's console, but you can do whatever you want here.

  console.log(insertedRowData);
}


You are applying .each() on the table, not on the cells. You need to add method getting specific descendants (in this case, table cells). Do it like that (jsfiddle):

function getRowValues() {
    var table = $("#tableBenef");
    $(table).find('td').each(function () {
        alert($(this).text());
    })
};

Note the additional .find('td') doing exactly what I described.

Was it helpful?

0

精彩评论

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

关注公众号