通过Dojo/jQuery/Javascript重新组织数组索引的最佳方法?
我列出了一个组件列表,用户可以随意添加/删除输入字段行,因此用户可以动态地自由删除/添加一行数据: ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----添加按钮---保存按钮---- 然而,这里出现了一个问题:数据被安排在html数组中,以便返回到JSP/sevlet模型,就像这样通过Dojo/jQuery/Javascript重新组织数组索引的最佳方法?,javascript,jquery,dojo,Javascript,Jquery,Dojo,我列出了一个组件列表,用户可以随意添加/删除输入字段行,因此用户可以动态地自由删除/添加一行数据: ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----|| | | | | | |移除按钮- ----添加按钮---保存按钮---- 然而,这里出现了一个问题:数据被安排在html数组中,以便返回到JSP/sevlet模型,就像这样 <input id="row[0]" .../>
<input id="row[0]" .../>
<input id="row[1]" .../>
<input id="row[2]" .../>
......
<input id="row[n]" .../>
所以问题是:当用户删除其中一个数组元素时,重新排列所有输入字段数组的最佳方式是什么?如果我正确理解您的问题,您是说从中间删除一个后,输入字段上的ID不准确,对吗?如果是这种情况,在删除元素后,类似这样的内容将更新id属性
<div>
<input id="row[0]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[1]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[2]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[3]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[4]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[5]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<script>
$('button.remove').click(function() {
$(this).parent('div').remove();
$('input.removableInput').each(function(index) {
$(this).attr('id', 'row[' + index + ']');
});
});
</script>
去除
去除
去除
去除
去除
去除
$('button.remove')。单击(函数(){
$(this.parent('div').remove();
$('input.removableInput')。每个(函数(索引){
$(this.attr('id','row['+index+']);
});
});
您能用它作为行的id吗?谢谢您的快速回复。您能否建议Dojo重新分配Dom的id?谢谢。我不是一个非常喜欢dojo的人,但是看起来你可以使用attr函数:。
<div>
<input id="row[0]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[1]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[2]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[3]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[4]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<div>
<input id="row[5]" type="text" class="removableInput"/><button class="remove">Remove</button>
</div>
<script>
$('button.remove').click(function() {
$(this).parent('div').remove();
$('input.removableInput').each(function(index) {
$(this).attr('id', 'row[' + index + ']');
});
});
</script>