删除整个表动态列和n列jquery

时间:2015-03-27 08:57:58

标签: jquery html-table

if (target >= 4) {
        $('.floor').slice(target).parent().parent().remove();
        //since I select the .floor input box, I have to use the parent() function two times, to move the selector up to the <tr> element
    } else {
        $('.floor').slice(4).parent().parent().remove();
        //since I select the .floor input box, I have to use the parent() function two times, to move the selector up to the <tr> element
    }

这是我删除n行的方式,具体取决于文本字段中的数字,然后是目标的值。 除了我需要删除整个列之外,我怎样才能创建相同的概念?

Update

<table id="flooring">
    <tr>
        <td><strong><p>Flooring</p></strong>
        </td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>
            <hr/>
        </td>
        <td>
            <span>1st Floor</span>

        </td>
        <td>
            <span>2nd Floor</span>

        </td>
        <td>
            <span>3rd Floor</span>

        </td>
        <td>
            <span>4th Floor</span>

        </td>
    </tr>
    <tr>
        <td><span>Reinforced Concrete</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="loor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>

    <tr>
        <td><span>Plain Cement</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Marble</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Wood</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Tiles</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>

</table>

这是默认的html,我还根据输入文本在此添加列。当前一个输入大于当前的时间时,我想要删除整个列的时间。

2 个答案:

答案 0 :(得分:1)

使用:nth-child选择器:

$("#tableid").find("tr :nth-child("+target+")").remove();

target开始删除所有列:

var col;
while ((col = $("#tableid").find("tr :nth-child("+target+")")).length) {
    col.remove();
}

答案 1 :(得分:0)

if (target >= 4) {
        $("table#.floor tr").find('td:eq('+target+')').nextAll().remove();
    } else {
        $("table#.floor tr").find('td:nth-child(5)').nextAll().remove();
    }

我最终使用了这段代码。希望这个小代码可以帮助某些人。上面的代码会选择目标为td的所有index和旁边的所有td