无法使用jquery在每个循环中正确删除表tr

时间:2014-12-31 11:28:16

标签: php jquery html

我的for循环是在运行时使用jquery

创建的
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_0"></tr>
<tr id="auctionLocation_1"></tr>
<tr id="auctionLocation_1"></tr>
<tr id="auctionLocation_1"></tr>

我正在使用jquery每个循环删除它,我的代码是

function testing() {
    var current = 0;
    $("#user").each(function() {
        $("#user").find("tr[id='auctionLocation_"+current+"']").remove();
        current++;
    });
}

OR

function testing() {
     $("#user").each(function(i, v) {
         $("#user").find("tr[id='auctionLocation_"+i+"']").remove();
        });
}

它只删除0索引而不是1索引,所以我该怎么办?

这就是我的创建代码

var counterForLocationsField = 1;
var auctionCompanyLocationArray = data.auctionCompanyLocationArray;
for(var auctionCompanyLocationLoop = 0; auctionCompanyLocationLoop < auctionCompanyLocationArray.length; auctionCompanyLocationLoop++) {
var completeTr = "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location Name "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationName"]+"</td> <td>Location Address "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationAddress"]+"</td></tr>";
completeTr += "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location City "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationCity"]+"</td> <td>Location State "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationState"]+"</td></tr>";
completeTr += "<tr id='auctionLocation_"+auctionCompanyLocationLoop+"'><td>Location Zipcode "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationZipcode"]+"</td> <td>Location Phone "+counterForLocationsField+"</td><td>"+auctionCompanyLocationArray[auctionCompanyLocationLoop]["auctionLocationPhone"]+"</td></tr>";
$(completeTr).insertBefore("#lastRow");
counterForLocationsField++;
}

2 个答案:

答案 0 :(得分:0)

当你选择id用户时,循环只循环一次。 Ids是独一无二的。 您可以使用带选择器的启动。 $("tr[id^='auctionLocation_']").remove(); 不需要循环。如果您想循环使用$("tr[id^='auctionLocation_']").each()

答案 1 :(得分:0)

不要对不同的元素使用相同的ID。 ID必须在页面中唯一。

使用类,例如:

<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_0"></tr>
<tr class="auctionLocation_1"></tr>
<tr class="auctionLocation_1"></tr>
<tr class="auctionLocation_1"></tr>

当你想删除它们时,只需执行上述操作,无需循环:

$('tr.auctionLocation_0').remove()