使用表格数据填写表格

时间:2013-10-29 06:51:05

标签: javascript php jquery html mysql

所以我有一个表单,我通过它向数据库添加条目,我试图使用相同的表单 更新/编辑数据库中的现有记录。

我的页面包含一个添加数据的表单和一个显示现有数据的表。 现有数据表对每个条目都有一列编辑,该表是使用php动态填充的。

这就是我想要的,当用户针对任何行单击编辑按钮时,该行中的数据应自动填充到表单中。

我制作了一个jsfiddle here

我尝试过的JS脚本:

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

    console.log(data);
}); // the event is not recognized. no action on clicking.

我在这里找到了很多技术stackoverflow,但是大多数技术都没有响应click事件或者它们返回null / empty值,那是因为表单是动态填充的吗?

我对网络开发很陌生,任何帮助都会受到赞赏。谢谢!

注意: 数据库包含20多条记录(动态添加),在Jsfiddle中我只显示了1条,以保持代码清洁。抱歉缺少css样式:P

1 个答案:

答案 0 :(得分:0)

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

    console.log(data);
});

使用这样可能会有所帮助

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

    console.log(data);
});