使用Jquery将记录添加到HTML表

时间:2013-07-19 11:56:54

标签: javascript jquery html css

我在jquery中找到了这个非常好的脚本,可以在双击时编辑表的内容。 现在我想通过添加按钮向表中添加更多功能。 我想添加的第一个功能是“添加”。 看看我的fiddle,事情就会很清楚

此刻一切似乎都运转正常。但是,当我点击添加时添加一行时,它不允许我像其他行一样编辑内容

HTML

<table class="editableTable">
    <thead>
        <tr>
            <th>Code</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>001</td>
            <td>Pedro Fernandes</td>
            <td>pedro.ferns@myemail.com</td>
            <td>(21) 9999-8888</td>
        </tr>     

    </tbody>
</table>
<td style="text-align:center;">
    <button onclick="addrecord()" >Add</button></td>

JQUERY

$(function () {
    $("td").dblclick(function () {
        var OriginalContent = $(this).text();

        $(this).addClass("cellEditing");
        $(this).html("<input type='text' value='" + OriginalContent + "' />");
        $(this).children().first().focus();

        $(this).children().first().keypress(function (e) {
            if (e.which == 13) {
                var newContent = $(this).val();
                $(this).parent().text(newContent);
                $(this).parent().removeClass("cellEditing");
            }
        });

        $(this).children().first().blur(function () {
            $(this).parent().text(OriginalContent);
            $(this).parent().removeClass("cellEditing");
        });
    });
});
function addrecord(){
      $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
}

4 个答案:

答案 0 :(得分:4)

更改

$("td").dblclick(function () {

$(".editableTable").on("dblclick", "td", function () {

两者之间的区别在于,前者将事件添加到现有TD,但不会在动态添加的任何TD上添加相同的事件,这是您要尝试实现的。然而,后者也会处理动态添加的任何TD。

答案 1 :(得分:0)

使其可双击的代码仅在开始时运行,因此代码不会应用于您创建的任何新行。一个非常脏的黑客只是将代码复制到你的函数中,尽管必然会有其他方法。

    function addrecord(){
      $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
      $("td").dblclick(function () {
        var OriginalContent = $(this).text();

        $(this).addClass("cellEditing");
        $(this).html("<input type='text' value='" + OriginalContent + "' />");
        $(this).children().first().focus();

        $(this).children().first().keypress(function (e) {
            if (e.which == 13) {
                var newContent = $(this).val();
                $(this).parent().text(newContent);
                $(this).parent().removeClass("cellEditing");
            }
        });

        $(this).children().first().blur(function () {
            $(this).parent().text(OriginalContent);
            $(this).parent().removeClass("cellEditing");
        });
    });
}

答案 2 :(得分:0)

确定您有静态表,在每个td上注册事件侦听器,然后添加新行。当然,在那一点上没有点击监听器。您需要在创建新行后注册侦听器:

var row = $('<tr><td>004</td><td></td><td></td><td></td></tr>');
row.find("td").dblclick(mylistener)
row.appendTo('table');

答案 3 :(得分:-1)

.live会做的伎俩..

$(function () {
$("td").live("dblclick",function () {
    var OriginalContent = $(this).text();

    $(this).addClass("cellEditing");
    $(this).html("<input type='text' value='" + OriginalContent + "' />");
    $(this).children().first().focus();

    $(this).children().first().keypress(function (e) {
        if (e.which == 13) {
            var newContent = $(this).val();
            $(this).parent().text(newContent);
            $(this).parent().removeClass("cellEditing");
        }
    });

    $(this).children().first().blur(function () {
        $(this).parent().text(OriginalContent);
        $(this).parent().removeClass("cellEditing");
    });
});
});
function addrecord(){
  $('<tr><td>004</td><td></td><td></td><td></td></tr>').appendTo('table');     
}