删除使用div创建的表中的一行

时间:2018-11-28 11:09:22

标签: javascript jquery

我是用PHP创建的表,并使用div来创建表,下面是表代码。

<div class="limiter">
    <div class="container-table100">
        <div class="wrap-table100">
            <div class="table">

                <div class="row header">
                    <div class="cell topLeft">
                        Customer Name
                    </div>
                    <div class="cell">
                        Target
                    </div>
                    <div class="cell">
                        File Type
                    </div>
                    <div class="cell">
                        Job Comment
                    </div>
                    <div class="cell">
                        Cust. ID
                    </div>
                    <div class="cell topRight">
                        Select Job
                    </div>
                </div>

                <?php while ($getJobsRow = $getJobs -> fetch(PDO::FETCH_ASSOC)){ ?>
                    <?php $loopCount++; //Count the loop run through time ?>
                <div class="row">
                    <div class="cell left <?php if ($numOfRows == $loopCount){ ?>bottomLeft<?php } //Set the CSS class if the loop is at the last row ?> " data-title="Full Name">
                        <?php echo $getJobsRow['customer_name']; ?>
                    </div>
                    <div class="cell" data-title="Age">
                        <?php echo $getJobsRow['Mal_Name']; ?>
                    </div>
                    <div class="cell" data-title="Job Title">
                        <?php echo $getJobsRow['FileExt']; ?>
                    </div>
                    <div class="cell" data-title="Location">
                        <?php echo $getJobsRow['Job_Comment']; ?>
                    </div>
                    <div class="cell" data-title="Location">
                        <?php echo $getJobsRow['customer_id']; ?>
                    </div>
                    <div class="cell right <?php if ($numOfRows == $loopCount){ ?>bottomRight<?php } ?> " data-title="Location">
                        <button class="selJobBtn w3-btn w3-blue-dark w3-round-medium w3-ripple" data-jid="<?php echo $getJobsRow['jId']; ?>">Select</button>
                    </div>
                </div>
                <?php } ?>

            </div>
        </div>
    </div>
</div>

我为每个数据行选择了按钮。我想在数据成功提交到表后隐藏或删除每一行。我使用jquery发送数据并禁用了按钮。

我的Jquery代码

$(document).ready(function () {
    $('.selJobBtn').on('click', function () {
        var selBtn = this;
        $.ajax({
            url: '../Functions/OpenjobLister_Function.php',
            type: 'get',
            data: {
                'jid':$(this).data('jid'),
                'uid':$('#uId').val()
            },
            dataType:'text',
            complete:function(data){
                if(data.responseText !== "1"){
                    alert("Data not added");
                }else{
                    $(selBtn).prop('disabled', true);
                    $('.row').parent('div').first().remove();
                }
            }
        });
    });
});

我尝试使用SO中提到的许多其他方法删除一行,但没有一个起作用。当前代码只是删除了整个表,我只是不知道如何删除一行。

2 个答案:

答案 0 :(得分:3)

那是因为您在文档中找到了整个表的任何.row的父div。尝试从单击的按钮中找到最接近的div.row并将其删除:

selBtn.closest('div.row').remove();

答案 1 :(得分:0)

使用HTML DOM元素删除该行:

这是您使用函数的方式,其中(0)是表行的索引。

document.getElementById("myTable").deleteRow(0); 
相关问题