将整个表行数据提取到javascript变量中

将整个表行数据提取到javascript变量中,javascript,php,html,css,web,Javascript,Php,Html,Css,Web,我有这样一个html表: | name | type | edit | | zzz | aaaa | edit | | xxx | bbbb | edit | | mmm | cccc | edit | 所以,我想做的是,如果我对第二行按edit,我想要数据“xxx” 和“bbb”存储在一些javascript变量中以供进一步使用 我找到了这段代码。但它只会给我点击的单元格,而不是整行 $('#tableID').click(function(e){ alert(

我有这样一个html表:

| name | type   | edit |
| zzz  | aaaa   | edit |
| xxx  | bbbb   | edit |
| mmm  | cccc   | edit |
所以,我想做的是,如果我对第二行按edit,我想要数据“xxx” 和“bbb”存储在一些javascript变量中以供进一步使用

我找到了这段代码。但它只会给我点击的单元格,而不是整行

$('#tableID').click(function(e){
    alert($(e.target).text()); // using jQuery
})
更新: 这是我的表格单元结构

<tr>
        <td> <?php echo $row['name'] ?> </td>
        <td> <?php echo $row['type'] ?></td>
        <td class="edit_button">Edit</td> 
</tr>
这将把表行存储在变量中

然后,您可以通过以下方式访问每个td小区:

$tr.find('td').each(function(){alert($(this).text())});
以下是一个示例:

这将把表行存储在变量中

然后,您可以通过以下方式访问每个td小区:

$tr.find('td').each(function(){alert($(this).text())});
以下是一个示例:

这将把表行存储在变量中

然后,您可以通过以下方式访问每个td小区:

$tr.find('td').each(function(){alert($(this).text())});
以下是一个示例:

这将把表行存储在变量中

然后,您可以通过以下方式访问每个td小区:

$tr.find('td').each(function(){alert($(this).text())});

下面是一个示例:

我将使用事件委派而不是
e.target
。只需给编辑按钮一个类

$("tableID").on("click", ".edit_button", function() {
    var data = $(this).closest("td").siblings().map(function() {
        return $(this).text();
    }).toArray();

    console.log(data);
});
现在,
data
将使用
edit
按钮保存单元格的同级数组


如果您不需要数组,但希望直接在循环中处理文本,我会这样做

$("tableID").on("click", ".edit_button", function() {
    $(this).closest("td").siblings().text(function(i, txt) {
        console.log(txt); // do something with the text
    });
});

我将使用事件委托而不是
e.target
。只需给编辑按钮一个类

$("tableID").on("click", ".edit_button", function() {
    var data = $(this).closest("td").siblings().map(function() {
        return $(this).text();
    }).toArray();

    console.log(data);
});
现在,
data
将使用
edit
按钮保存单元格的同级数组


如果您不需要数组,但希望直接在循环中处理文本,我会这样做

$("tableID").on("click", ".edit_button", function() {
    $(this).closest("td").siblings().text(function(i, txt) {
        console.log(txt); // do something with the text
    });
});

我将使用事件委托而不是
e.target
。只需给编辑按钮一个类

$("tableID").on("click", ".edit_button", function() {
    var data = $(this).closest("td").siblings().map(function() {
        return $(this).text();
    }).toArray();

    console.log(data);
});
现在,
data
将使用
edit
按钮保存单元格的同级数组


如果您不需要数组,但希望直接在循环中处理文本,我会这样做

$("tableID").on("click", ".edit_button", function() {
    $(this).closest("td").siblings().text(function(i, txt) {
        console.log(txt); // do something with the text
    });
});

我将使用事件委托而不是
e.target
。只需给编辑按钮一个类

$("tableID").on("click", ".edit_button", function() {
    var data = $(this).closest("td").siblings().map(function() {
        return $(this).text();
    }).toArray();

    console.log(data);
});
现在,
data
将使用
edit
按钮保存单元格的同级数组


如果您不需要数组,但希望直接在循环中处理文本,我会这样做

$("tableID").on("click", ".edit_button", function() {
    $(this).closest("td").siblings().text(function(i, txt) {
        console.log(txt); // do something with the text
    });
});

我试着…检查我的更新。没有警报,我想没有检测到单击。@user2529058:您的
alert()
调用在事件处理程序之外。把它们放进去。这是日志控制台所说的:uncaughttypeerror:无法设置的属性“value”null@user2529058:我的答案没有使用名为“value”的属性。你在回答中发布的新代码也没有。我试着…检查我的更新。没有警报,我想没有检测到单击。@user2529058:您的
alert()
调用在事件处理程序之外。把它们放进去。这是日志控制台所说的:uncaughttypeerror:无法设置的属性“value”null@user2529058:我的答案没有使用名为“value”的属性。你在回答中发布的新代码也没有。我试着…检查我的更新。没有警报,我想没有检测到单击。@user2529058:您的
alert()
调用在事件处理程序之外。把它们放进去。这是日志控制台所说的:uncaughttypeerror:无法设置的属性“value”null@user2529058:我的答案没有使用名为“value”的属性。你在回答中发布的新代码也没有。我试着…检查我的更新。没有警报,我想没有检测到单击。@user2529058:您的
alert()
调用在事件处理程序之外。把它们放进去。这是日志控制台所说的:uncaughttypeerror:无法设置的属性“value”null@user2529058:我的答案没有使用名为“value”的属性。你在回答中发布的新代码也没有。