Jquery 动态更改元素的id

Jquery 动态更改元素的id,jquery,Jquery,我正在添加元素。删除每个元素时,我希望更新其余元素 <div id="rowCount2"> <div id="rowCount3"> <div id="rowCount4"> <div id="rowCount5"> <div id="rowCount6"> 比如说 这是五个要素 <div id="rowCount2"> <div id="rowCount3"> <div id="rowCount4"&

我正在添加元素。删除每个元素时,我希望更新其余元素

<div id="rowCount2">
<div id="rowCount3">
<div id="rowCount4">
<div id="rowCount5">
<div id="rowCount6">
比如说

这是五个要素

<div id="rowCount2">
<div id="rowCount3">
<div id="rowCount4">
<div id="rowCount5">
<div id="rowCount6">

如果我删除id为rowCount4的元素,则rowCount5和rowCount6后面的元素应被更改为rowCount4和rowCount5

我试过跟随,但这不起作用

 for(var k = rowCount; k < 7; k++){
    $('#rowCount'+k).attr('id','rowCount'+(k-1));
 }
for(var k=rowCount;k<7;k++){
$('#rowCount'+k).attr('id','rowCount'+(k-1));
}
更新

删除函数

function removeRow(removeNum) {

            var row = $('#rowCount' + removeNum);
            var sel = row.find(':selected').val();
            var name = row.find(':selected').text();
            addRemoved(sel);
            console.log(field_options);

            $('.loop-count').each(
                    function () {
                        var selectCountId = ($(this).attr("id"));
                        myList = [];
                        $('#' + selectCountId).children('option').each(function () {
                            myList.push($(this).val())
                        });
                        if (myList.indexOf(sel) == -1) {
                            $('<option>').val(sel).text(name).appendTo('#' + selectCountId);
                        }
                    });
            row.remove();
            rowCount--;
}
函数removeRow(removeNum){
变量行=$(“#行计数”+removeNum);
var sel=row.find(':selected').val();
var name=row.find(':selected').text();
删除的地址(sel);
console.log(字段_选项);
$('.loop count')。每个(
函数(){
var selectCountId=($(this.attr(“id”));
myList=[];
$('#'+selectCountId).children('option').each(函数(){
myList.push($(this.val())
});
if(myList.indexOf(sel)=-1){
$('').val(sel).text(name).appendTo('#'+selectCountId);
}
});
row.remove();
行数--;
}
因为你有2个,作为起点

另外,您可以使用索引并应用另一个数字进行递增,但这样您将始终保持(+1)结构

因为你有2个,作为起点

另外,您可以使用索引并应用另一个数字进行递增,但这样您将始终保持(+1)结构

因为你有2个,作为起点

另外,您可以使用索引并应用另一个数字进行递增,但这样您将始终保持(+1)结构

因为你有2个,作为起点

另外,您可以使用索引并应用另一个数字进行递增,但这样您将始终保持(+1)结构…

试试看

函数removeRow(removeNum){
变量行=$(“#行计数”+removeNum);
var sel=row.find(':selected').val();
var name=row.find(':selected').text();
删除的地址(sel);
console.log(字段_选项);
$('.loop count')。每个(函数(){
var selectCountId=($(this.attr(“id”));
myList=[];
$('#'+selectCountId).children('option').each(function(){
myList.push($(this.val())
});
if(myList.indexOf(sel)=-1){
$('').val(sel).text(name).appendTo('#'+selectCountId);
}
});
row.nextAll('div[id^=“rowCount”]').attr('id',函数(i,id){
返回'rowCount'+(id.replace('rowCount','')-1;
})
row.remove();
行数--;
}
var rowCount=6,
现场检查选项;
(三)
函数addRemoved(){}

1.
2.
3.
4.
5.
6
试试看

函数removeRow(removeNum){
变量行=$(“#行计数”+removeNum);
var sel=row.find(':selected').val();
var name=row.find(':selected').text();
删除的地址(sel);
console.log(字段_选项);
$('.loop count')。每个(函数(){
var selectCountId=($(this.attr(“id”));
myList=[];
$('#'+selectCountId).children('option').each(function(){
myList.push($(this.val())
});
if(myList.indexOf(sel)=-1){
$('').val(sel).text(name).appendTo('#'+selectCountId);
}
});
row.nextAll('div[id^=“rowCount”]').attr('id',函数(i,id){
返回'rowCount'+(id.replace('rowCount','')-1;
})
row.remove();
行数--;
}
var rowCount=6,
现场检查选项;
(三)
函数addRemoved(){}

1.
2.
3.
4.
5.
6
试试看

函数removeRow(removeNum){
变量行=$(“#行计数”+removeNum);
var sel=row.find(':selected').val();
var name=row.find(':selected').text();
删除的地址(sel);
console.log(字段_选项);
$('.loop count')。每个(函数(){
var selectCountId=($(this.attr(“id”));
myList=[];
$('#'+selectCountId).children('option').each(function(){
myList.push($(this.val())
});
if(myList.indexOf(sel)=-1){
$('').val(sel).text(name).appendTo('#'+selectCountId);
}
});
row.nextAll('div[id^=“rowCount”]').attr('id',函数(i,id){
返回'rowCount'+(id.replace('rowCount','')-1;
})
row.remove();
行数--;
}
var rowCount=6,
现场检查选项;
(三)
函数addRemoved(){}

1.
2.
3.
4.
5.
6
试试看

函数removeRow(removeNum){
变量行=$(“#行计数”+removeNum);
var sel=row.find(':selected').val();
var name=row.find(':selected').text();
删除的地址(sel);
console.log(字段_选项);
$('.loop count')。每个(函数(){
var selectCountId=($(this.attr(“id”));
myList=[];
$('#'+selectCountId).children('option').each(function(){
myList.push($(this.val())
});
if(myList.indexOf(sel)=-1){
$('').val(sel).text(name).appendTo('#'+selectCountId);
}
});
row.nextAll('div[id^=“rowCount”]').attr('id',函数(i,id){
返回'rowCount'+(id.replace('rowCount','')-1;
})
row.remove();
行数--;
}
var rowCount=6,
现场检查选项;
(三)
函数addRemoved(){}

1.
2.
3.
4.
5.

6
下面是一个示例,说明如何更改剩余元素上的
id
属性的值

<div id="rowCount2">
<div id="rowCount3">
<div id="rowCount4">
<div id="rowCount5">
<div id="rowCount6">
$('.container')。在('click','a',function()上{
var crntId=$(this.parent().attr('id').replace('rowCount',''),
元素=$(this.parent().nextUntil($(this.parent());
if($(this.parent().next().length!=0){
每个元素(功能(索引){
$(本)
.html((parseInt(crntId)+索引)+'')
.attr('id','rowCount'+(parseInt(crntId)+index));
});
$(this.parent().remove();
}否则{
$(this.parent().remove();
}
});
.container div{
保证金:0自动;