删除具有相同类属性但保留一个的所有元素

Sur*_*ily 3 html javascript jquery html5 dom

我可以使用带有jquery remove函数的名为trSkillCls的类删除所有文本框.我想要实现的是,如果有5个具有相同类名的文本框我不想删除所有这些,但只有4应该删除,即总是少一个元素.我必须实际编写一个函数来删除所有文本字段,只留下一个文本字段保留在单击保存按钮上.这是我的代码:

$("#addAnotherSkillBtn").click(function(){
  addAnotherSkill();
 });    

function addAnotherSkill(){

  var trSkillHTML = $("<div />").append($("#trSkill").clone()).html();                      
  $("#tBSkill").append(trSkillHTML);                        
}

function removeSkill(self){
  var delBtnCtr = $('#tBSkill').find('.deleteSkillCls').length;                     
  if(delBtnCtr > 1)
$(self).closest('tr').remove();
}
Run Code Online (Sandbox Code Playgroud)

和HTML:

<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
    <td ><strong>Employee</strong></td>
    <td width="2%">:</td>
    <td width="82%"><input name="empName" id="empName" type="text" style="width:100%;height:30" maxlength="30"  ></td>
</tr>                                       

<tr>

    <table  class="skillTable" border="0" cellpadding="0" cellspacing="0" width="480">
        <tbody id="tBSkill">
            <tr id="trTitle">
                <td width="206"><strong>Skill</strong></td>

                <td width="270"><strong>Level</strong></td>
                <td></td>
            </tr>

            <tr id="trSkill" class="trSkillCls">
                <td><input  name="skill" id="skillP0" style="height:30;width:190;" maxlength="60" autocomplete="off" tooltip="Please enter only one IT skill per box." type="text"></td>
                <td >
                    <select name="ddlSkillLevel" class="w180">
                        <option value="-1">Level</option>  
                        <option value="00" selected="selected">Beginner</option>  
                        <option value="01">Intermediate</option>  
                        <option value="02" >Expert</option> 
                        <!-- <option value="03">3</option>  <option value="04">4</option>  <option value="05">5</option>  <option value="06">6</option>  <option value="07">7</option>  <option value="08">8</option>  <option value="09">9</option>  <option value="10">10</option>  <option value="11">11</option>            -->
                    </select> 

                </td>

                <td>
                    <input type="button" class="deleteSkillCls" name='parentDel' onclick="removeSkill(this)" value="Delete">
                </td>

            </tr>
        </tbody>
    </table>
</tr>
<tr>
    <td></td>
</tr>
<tr>
    <td>
        <input type="hidden" id="str" name="str" value="" /> 
        <input type="button" name="addAnotherSkill" id='addAnotherSkillBtn' value="Add Another Skill">
        <input type="submit" name="submit" id="btnSave" value="Save"> 
        <input type="reset" name="reset" value="Reset"></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

Bhu*_*kar 6

您可以使用以下jQuery删除所有内容(假设没有特定标准保留哪个):

$('.trSkillCls').not(':first').remove();
Run Code Online (Sandbox Code Playgroud)

演示