【问题标题】:Jquery add and remove table rowsjquery添加和删除表行
【发布时间】:2013-04-18 11:35:38
【问题描述】:

我有一个使用 jQuery 动态添加和删除行的 html 表。行数受 jQuery 计数器的限制,允许用户最多添加 4 行。我的问题是,当用户创建第 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));
}

【问题讨论】:

    标签: javascript jquery html-table


    【解决方案1】:

    大量修复,

    1. 删除了删除按钮的额外处理程序
    2. 将按钮 ID 更改为类,因为您不应重复 ID。 Read why you should not duplicate ID
    3. 添加了启用“添加行”按钮的逻辑。请参阅 Fixed fiddle
    4. 删除了 Add Row 处理程序中的 var 声明以更新全局 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");
    });
    

    【讨论】:

    • 感谢您提供额外的详细信息和帮助。
    【解决方案2】:

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

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

    【讨论】:

    • 非常感谢您的帮助和快速响应!效果很好。
    • @nallad1985 你不应该重复 ID.. 改用类选择器。
    【解决方案3】:

    单击删除 btn 时,您应该减少计数器编号并启用按钮和属性值

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

    【讨论】:

      【解决方案4】:

      我更新了你的 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");
      });
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-30
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 2013-09-07
        • 2011-07-24
        • 2011-03-24
        相关资源
        最近更新 更多