Javascript 删除并更新动态创建的表行ID';s串行使用Jquery
例如,我在表中列出了使用ajax、jquery销售的产品。其中每个表行都获得动态id,如第1行、第2行、第3行等 可以选择删除任何行,例如删除第二行(第2行)Javascript 删除并更新动态创建的表行ID';s串行使用Jquery,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,例如,我在表中列出了使用ajax、jquery销售的产品。其中每个表行都获得动态id,如第1行、第2行、第3行等 可以选择删除任何行,例如删除第二行(第2行) 因此,我想要的是,在删除行之后,表行id也应该得到更新,可能一个函数会这样做,例如,我不希望它是行1,行3,而是我希望它是行1,行2。我已经在我的项目中的某个地方使用了这段代码,根据您的需要进行了一些调整 Jquery部分 <script src="http://ajax.googleapis.com/ajax/libs/jquer
因此,我想要的是,在删除行之后,表行id也应该得到更新,可能一个函数会这样做,例如,我不希望它是行1,行3,而是我希望它是行1,行2。我已经在我的项目中的某个地方使用了这段代码,根据您的需要进行了一些调整 Jquery部分
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".remove_tr").click(function(){
var tr_id = $(this).parent().attr("id");
var flag = "N"; var row_cnt = 0;
$("#dummy_table tr").each(function(){
if( flag == "Y" ){
$(this).attr("id", "row_"+ row_cnt);
$(this).children("td:first-child").html("row_"+ row_cnt);
$(this).attr("id", "row_"+ row_cnt);
row_cnt++;
}
if( flag =="N" && $(this).attr("id") == tr_id){
var rowArr = $(this).attr("id").split("_");
row_cnt = rowArr[1];
$(this).remove(); flag= "Y";
}
})
});
});
</script>
<table id="dummy_table">
<?php for($i = 0; $i < 10; $i++){ ?>
<tr id="row_<?php echo $i; ?>">
<td>row_<?php echo $i; ?></td>
<td class="remove_tr">remove</td>
</tr>
<?php } ?>
</table>
$(文档).ready(函数(){
$(“.remove_tr”)。单击(函数(){
var tr_id=$(this.parent().attr(“id”);
var flag=“N”var行\u cnt=0;
$(“#dummy_table tr”)。每个(函数(){
如果(标志==“Y”){
$(this.attr(“id”,“row_u”+row_cnt);
$(this.children(“td:first child”).html(“row_u”+row_cnt);
$(this.attr(“id”,“row_u”+row_cnt);
行_cnt++;
}
如果(flag==“N”&&&$(this.attr(“id”)==tr_id){
var rowArr=$(this.attr(“id”).split(“”);
row_cnt=rowArr[1];
$(this.remove();flag=“Y”;
}
})
});
});
HTML部分
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".remove_tr").click(function(){
var tr_id = $(this).parent().attr("id");
var flag = "N"; var row_cnt = 0;
$("#dummy_table tr").each(function(){
if( flag == "Y" ){
$(this).attr("id", "row_"+ row_cnt);
$(this).children("td:first-child").html("row_"+ row_cnt);
$(this).attr("id", "row_"+ row_cnt);
row_cnt++;
}
if( flag =="N" && $(this).attr("id") == tr_id){
var rowArr = $(this).attr("id").split("_");
row_cnt = rowArr[1];
$(this).remove(); flag= "Y";
}
})
});
});
</script>
<table id="dummy_table">
<?php for($i = 0; $i < 10; $i++){ ?>
<tr id="row_<?php echo $i; ?>">
<td>row_<?php echo $i; ?></td>
<td class="remove_tr">remove</td>
</tr>
<?php } ?>
</table>
我希望这段代码已经在我的项目中的某个地方出现了,只是根据您的需要进行了一些调整
Jquery部分
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".remove_tr").click(function(){
var tr_id = $(this).parent().attr("id");
var flag = "N"; var row_cnt = 0;
$("#dummy_table tr").each(function(){
if( flag == "Y" ){
$(this).attr("id", "row_"+ row_cnt);
$(this).children("td:first-child").html("row_"+ row_cnt);
$(this).attr("id", "row_"+ row_cnt);
row_cnt++;
}
if( flag =="N" && $(this).attr("id") == tr_id){
var rowArr = $(this).attr("id").split("_");
row_cnt = rowArr[1];
$(this).remove(); flag= "Y";
}
})
});
});
</script>
<table id="dummy_table">
<?php for($i = 0; $i < 10; $i++){ ?>
<tr id="row_<?php echo $i; ?>">
<td>row_<?php echo $i; ?></td>
<td class="remove_tr">remove</td>
</tr>
<?php } ?>
</table>
$(文档).ready(函数(){
$(“.remove_tr”)。单击(函数(){
var tr_id=$(this.parent().attr(“id”);
var flag=“N”var行\u cnt=0;
$(“#dummy_table tr”)。每个(函数(){
如果(标志==“Y”){
$(this.attr(“id”,“row_u”+row_cnt);
$(this.children(“td:first child”).html(“row_u”+row_cnt);
$(this.attr(“id”,“row_u”+row_cnt);
行_cnt++;
}
如果(flag==“N”&&&$(this.attr(“id”)==tr_id){
var rowArr=$(this.attr(“id”).split(“”);
row_cnt=rowArr[1];
$(this.remove();flag=“Y”;
}
})
});
});
HTML部分
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".remove_tr").click(function(){
var tr_id = $(this).parent().attr("id");
var flag = "N"; var row_cnt = 0;
$("#dummy_table tr").each(function(){
if( flag == "Y" ){
$(this).attr("id", "row_"+ row_cnt);
$(this).children("td:first-child").html("row_"+ row_cnt);
$(this).attr("id", "row_"+ row_cnt);
row_cnt++;
}
if( flag =="N" && $(this).attr("id") == tr_id){
var rowArr = $(this).attr("id").split("_");
row_cnt = rowArr[1];
$(this).remove(); flag= "Y";
}
})
});
});
</script>
<table id="dummy_table">
<?php for($i = 0; $i < 10; $i++){ ?>
<tr id="row_<?php echo $i; ?>">
<td>row_<?php echo $i; ?></td>
<td class="remove_tr">remove</td>
</tr>
<?php } ?>
</table>
很简单。运行下面的代码段
$(“.remove”)。单击(函数(){
$(this.parent().remove();
var计数器=1;
$(“#foo tr”)。每个(函数(){
$(this.attr('id','row.'+计数器);
$(this.find('td:first child').html('row_'+counter);//仅显示结果
计数器++;
});
});代码>
第1排
去除
第2排
去除
第3排
去除
第4排
去除
这很简单。运行下面的代码段
$(“.remove”)。单击(函数(){
$(this.parent().remove();
var计数器=1;
$(“#foo tr”)。每个(函数(){
$(this.attr('id','row.'+计数器);
$(this.find('td:first child').html('row_'+counter);//仅显示结果
计数器++;
});
});代码>
第1排
去除
第2排
去除
第3排
去除
第4排
去除
您的代码是什么样子的?你介意和我分享一下吗?可以使用静态的JSON/Array
来代替AJAX
请求..不,我需要AJAX用于其他目的..总之,其他答案可以帮助我解决:)那么您的代码是什么样子的?你介意和我分享一下吗?可以使用静态的JSON/Array
来代替AJAX
请求..不,我需要AJAX做其他用途..无论如何,其他答案帮助我整理:)这正是我想要的…非常感谢:)这正是我想要的…非常感谢:)是的,你的代码运行良好..谢谢..感谢你的帮助:)是的,你的代码运行良好..谢谢..感谢你的帮助:)