jQuery添加和删除表格行。

5
我有一个 HTML 表格,使用 jQuery 动态添加和删除行。行数由 jQuery 计数器限制,允许用户添加多达 4 行。我的问题是,当用户创建第四行时,他们已经达到了限制,但当他们删除一行时,限制仍然存在,他们无法再添加任何行。 http://jsfiddle.net/nallad1985/sqrrt/ HTML
<table id="myTable" class="order-list">
<thead>
    <tr>
        <td>Name</td>
        <td>Price</td>
    </tr>
</thead>
<tbody>
    <tr>
        <td>
            <input type="text" name="name" />
        </td>
        <td>
            <input type="text" name="price1" />
        </td>
        <td><a class="deleteRow"></a>
        </td>
    </tr>
</tbody>
<tfoot>
    <tr>
        <td colspan="5" style="text-align: left;">
            <input type="button" id="addrow" value="Add Row" />
        </td>
    </tr>
    <tr>
        <td colspan="">Grand Total: $<span id="grandtotal"></span>

        </td>
    </tr>
</tfoot>

JQUERY

$(document).ready(function () {
var counter = 0;
$("#addrow").on("click", function () {
    var counter = $('#myTable tr').length - 2;
    var newRow = $("<tr>");
    var cols = "";

    cols += '<td><input type="text" name="name' + counter + '"/></td>';
    cols += '<td><input type="text" name="price' + counter + '"/></td>';

    cols += '<td><input type="button" id="ibtnDel"  value="Delete"></td>';
    newRow.append(cols);
    if (counter == 4) $('#addrow').attr('disabled', true).prop('value', "You've reached the limit");
    $("table.order-list").append(newRow);
    counter++;
});

$("table.order-list").on("change", 'input[name^="price"]', function (event) {
    calculateRow($(this).closest("tr"));
    calculateGrandTotal();
});

$("table.order-list").on("click", "#ibtnDel", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();
});

});

function calculateRow(row) {
var price = +row.find('input[name^="price"]').val();

}

function calculateGrandTotal() {
var grandTotal = 0;
$("table.order-list").find('input[name^="price"]').each(function () {
    grandTotal += +$(this).val();
});
$("#grandtotal").text(grandTotal.toFixed(2));
}
4个回答

8

更新如下:

  1. 删除了多余的删除按钮处理程序。
  2. 将按钮 ID 更改为 class,因为不应该重复使用 ID。 阅读为什么不应该重复使用 ID
  3. 添加了逻辑以启用添加行按钮。请参见修正后的示例
  4. 删除了 Add Row 处理程序中的 var 声明以更新全局变量。

http://jsfiddle.net/sqrrt/2/

$("table.order-list").on("click", ".ibtnDel", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();

    counter -= 1
    $('#addrow').attr('disabled', false).prop('value', "Add Row");
});

感谢您提供的额外细节和帮助。 - nallad1985

2

当您删除一行时,只需重新启用按钮并将计数器减少:

$("table.order-list").on("click", "#ibtnDel", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();
    counter--;
    $('#addrow').prop('disabled', false).prop('value', "Add row");
});

非常感谢您的帮助和快速响应!!那个完美地起作用了。 - nallad1985
@nallad1985,你不应该重复使用ID..而是应该使用类选择器。 - Selvakumar Arumugam

2

点击删除按钮后,您应该减少计数器数量,并启用按钮和属性值。

$("table.order-list").on("click", "#ibtnDel", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();
    counter = counter-1;
    $("#addrow").attr("disabled", false).prop("value", "Add Row")

});

2
我更新了你的Javascript代码,请在Fiddle上查看:

http://jsfiddle.net/sqrrt/3/

$("table.order-list").on("click", "#ibtnDel", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();
    counter --;
    if (counter < 5) $('#addrow').attr("disabled", false).prop('value', "Add Row");
});

问题在于,您没有正确地倒计时计数器,并且您的删除按钮方法没有被调用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接