例如,我在列表中使用ajax,jquery列出待售產品。其中每個表格行都獲得動態ID類似,row_1,row_2,row_3等刪除並更新動態創建的錶行ID使用Jquery連續使用
有一個選項可以刪除任何行,例如第二行(row_2)被刪除。
所以我想要的是,行被刪除後,錶行ID也應該得到更新,也許一個函數會做到這一點,例如我不希望它是row_1,row_3而是我希望它是row_1,row_2。
例如,我在列表中使用ajax,jquery列出待售產品。其中每個表格行都獲得動態ID類似,row_1,row_2,row_3等刪除並更新動態創建的錶行ID使用Jquery連續使用
有一個選項可以刪除任何行,例如第二行(row_2)被刪除。
所以我想要的是,行被刪除後,錶行ID也應該得到更新,也許一個函數會做到這一點,例如我不希望它是row_1,row_3而是我希望它是row_1,row_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>
HTML部分
<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").click(function() {
$(this).parent().remove();
var counter = 1;
$("#foo tr").each(function() {
$(this).attr('id', 'row_'+counter);
$(this).find('td:first-child').html('row_'+counter); //just to show the result
counter++;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="foo">
<tr id="row_1" class="block" >
<td>row_1</td>
<td class="remove">remove</td>
</tr> <br>
<tr id="row_2" class="block" >
<td>row_2</td>
<td class="remove">remove</td>
</tr> <br>
<tr id="row_3" class="block" >
<td>row_3</td>
<td class="remove">remove</td>
</tr> <br>
<tr id="row_4" class="block" >
<td>row_4</td>
<td class="remove">remove</td>
</tr> <br>
</table>
是你的代碼工作正常..謝謝你..讚賞你的幫助:) –
以及如何做你的代碼是什麼樣子?你會介意分享嗎?一個靜態的'JSON /數組'可以用來代替'AJAX'請求。 – Rayon
不,我需要其他用途的Ajax ..反正,其他答案幫我把它整理出來:) –