如何删除HTML表单中除最后一个元素之外的所有元素

时间:2016-07-18 03:49:44

标签: javascript jquery

我有一个html表格如下

<div class="control-group" id="fields">
    <label for="">Use this section to add as many execution steps as you would like for this test case.</label>
    <div class="controls">
        <form role="form" autocomplete="off" id="formStep">
            <div class="entry input-group col-sm-12" style="margin-bottom:3px;">
                <input class="form-control stepFields" name="fields[]" type="text" placeholder="test step">
            </div>
            <div class="entry input-group col-sm-12" style="margin-bottom:3px;">
                <input class="form-control stepFields" name="fields[]" type="text" placeholder="test step">
            </div>
            <div class="entry input-group col-sm-12" style="margin-bottom:3px;">
                <input class="form-control stepFields" name="fields[]" type="text" placeholder="test step">
            </div>
            <div class="entry input-group col-sm-12" style="margin-bottom:3px;">
                <input class="form-control stepFields" name="fields[]" type="text" placeholder="test step">
            </div>
        </form>
        <br>

    </div>
</div>

我有以下JS(Jquery)

    $('.controls form')
        .not($(this).children('.entry').last()[0])
        .remove(); 

我想要删除除了最后一个之外的所有类名为entry 的div?

你能告诉我这里我做错了什么吗?

谢谢

1 个答案:

答案 0 :(得分:2)

使用find()not()

的组合

尝试以下

 $('.controls form').find('.entry').not(':last').remove(); 

demo