使用jquery表中的复选框从数组中删除JavaScript对象

问题描述 投票:0回答:1

我正在处理项目我遇到了一个问题:我创建了jQuery表并且在复选框上检查了特定行的所有值并将其推入数组现在我想从取消选中该行时从数组中删除每一行记录。我已经编写了一些删除脚本,但它无法正常工作。并告诉我,有任何简单的方法来获取特定行的数据与jQuery表中的复选框。

脚本

$.ajax({
    type: 'POST',
    url: "/Student/GetFee",
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    data: JSON.stringify({ Class_Id: Class_Id }),
    success: function (list) {
        $("#view").html('');
        $.each(list, function (key, value) {
            var html = $(
                '<tr>' +
                    '<td>' + value.Id + '</td>' +
                    '<td>' + value.FeeHeadName + '</td>' +
                    '<td>' + value.SchoolName + '</td>' +
                    '<td>' + value.ClassName + '</td>' +
                    '<td>' + value.Amount + '</td>' +
                    '<td>' + "<input type='text'  name='Paid' class='paid' onkeypress='paid(this)'/>" + '</td>' +
                    '<td>' + "<input type='text' class='Discount' name='Discount' onkeypress='discount(this)'/>" + '</td>' +
                    '<td>' + "<input type='text' class='NetAmount' name='NetAmount' />" + '</td>' +
                    '<td>' + "<input type='checkbox' class='ckb'/>" + '</td>' +
                '</tr>');
            $("#view").append(html);
            html = '';
        });

        //pushing objects in array
        $('#view input[type=checkbox]').click(function () {                                             
            var row = $(this).closest('tr');
            var id = row.find('td:eq(0)').text();
            var fname = row.find('td:eq(1)').text();
            var sname = row.find('td:eq(2)').text();
            var cname = row.find('td:eq(3)').text();
            var amount = row.find('td:eq(4)').text();
            var paid = row.find($('.paid')).val();
            var discount = row.find($('.Discount')).val();
            var netAmount = row.find($('.NetAmount')).val();
                            
            if ($(this).is(":checked")) {
                info.push({                                 
                    Fee_Id: id,
                    FeeHeadName: fname,
                    sname: sname,
                    cname: cname,
                    Amount: amount,
                    Paid: paid,
                    Discount: discount,
                    NetAmount: netAmount
                });                            
                console.log(info);
            }
            else {
                if ($(this).not(":checked")) {
                    var x = info.indexOf($(this).val());
                    info.splice(x, 1);
                    console.log(info);
                }
            }
        });
    }
});

Take a look of View

javascript jquery arrays asp.net-mvc checkbox
1个回答
0
投票

你必须在数组中循环并检查哪一个具有未检查行的id

if ($(this).not(":checked")) {
    var indexToRemove = -1;

    for (var i = 0; i < info.length; i++) {
        if (info[i].Fee_id == id) {
            indexToRemove = i;
            break;
        }
    }
    if (indexToRemove != -1) {
        info.splice(indexToRemove, 1);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.