好的,我在下面做的是在用户点击id #ds时添加一个输入字段。而这增加了问题是,当我尝试删除最后一个它没有删除它。有些人可以让我知道我做错了什么。我是javascript的新手。
var i=3; var ime;
$('#ds').click(function(){
i++;
switch(i){
case 4: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 5: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 6: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 7: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 8: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 9: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 10: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">'; break;
case 11: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 12: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 13: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';
break;
case 14: ime='<input type="text" name="weight" placeholder="Pallet Weight (lbs)" id="QT" class="weight"> <input type="text" name="length" placeholder="Length (in)" id="QT"><input type="text" name="width" placeholder="Width (in)" id="QT"><input type="text" name="height" placeholder="Height (in)" id="QT">';i=0;
break;
}
$('ul').append('<li id="Pallet1" class="inactive"><span class="itemNumber">'+i+'</span>'+ime+'</li>');
});
$('#us').click(function(){
i--;
$('li:last').prev().remove();
});
答案 0 :(得分:0)
这可能与此代码有关:
$('#us').click(function(){
i--;
$('li:last').prev().remove();
});
由于您要删除上一个 li
,当剩下 1 li
时, 0 上一页 li
要删除。因此,最后li
停留在那里。
所以你可以尝试这样的事情
$('#us').click(function(){
i--;
$('li:last').prev().remove();
if ($('li:last').prev().length == 0 || jQuery.isEmptyObject($('li:last').prev()) == true) {
$('li:last').remove();
}
});
答案 1 :(得分:0)
这可能会对您有所帮助:
$('#us').click(function () {
i--;
if($('li:last').prev().length>0)
$('li:last').prev().remove();
else
$('li:last').remove();
});