索引编号的JavaScript克隆表单元素问题
JavaScript clone form element issue with index numbering
我有一个表格,我想在其中记录一个练习集,然后根据需要添加新的练习集。这一部分工作得很好,但如果删除上一个以外的集合,就会抛出集合编号。
HTML:
<div class="form-group">
<tbody class="tbodyClone">
<tr id="clonedInput1" class="clonedInput">
<td><h4 name="set" class="heading-reference">Set 1</h4>
<select id="style" class="form-control">
<option>Pull ups</option>
<option>Push ups</option>
</select></td>
<td><select id="weight" class="form-control">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select></td>
<td><select id="reps" class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
</select></td>
<td>
<button id="btnAdd_0" name="btnAdd_0" type="button" class="clone btn btn-success"><i class="fa fa-plus-circle"></i></button>
<button id="btnDel_0" name="btnDel_0" type="button" class="remove btn btn-danger"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
</tbody>
</table>
JavaScript:
var regex = /^(.*)('d)+$/i;
var cloneIndex = $(".clonedInput").length;
if ($(".clonedInput").length == 1) {
$('.remove').hide();
} else {
$('.remove').show();
}
function clone() {
cloneIndex++;
$(this).parents(".clonedInput").clone()
.appendTo(".tbodyClone")
.attr("id", "clonedInput" + cloneIndex)
.find(".heading-reference").text('Set ' + cloneIndex)
.on('click', 'clone', clone)
.on('click', 'remove', remove);
//delete
console.log("Total lines => " + $(".clonedInput").length);
if ($(".clonedInput").length == 1) {
$('.remove').hide();
} else {
$('.remove').show();
}
}
function remove() {
$(this).parents(".clonedInput").remove();
cloneIndex--;
if ($(".clonedInput").length == 1) {
$('.remove').hide();
} else {
$('.remove').show();
}
}
$(document).on("click", ".clone", clone);
$(document).on("click", ".remove", remove);
即使一个集合被删除了,我有什么方法可以按顺序保持集合编号吗?
将其合并到现有代码中最简单的方法是在删除一行之后重构以下所有行的ID。
function remove() {
var toRemove = $(this).closest(".clonedInput");
// which ID are we removing?
var removedId = parseInt(toRemove.attr('id').replace("clonedInput",""));
// .nextAll will take all following siblings, that match the selector.
// this avoids reassigning IDs to entries in front of the removed one...
// ...since they don't change anyway
toRemove.nextAll('.clonedInput').each(function(){
$(this).attr('id', "clonedInput"+(removedId)).find(".heading-reference").text('Set ' + removedId)
removedId++;
});
toRemove.remove();
cloneIndex--;
if ($(".clonedInput").length == 1) {
$('.remove').hide();
} else {
$('.remove').show();
}
}
没有测试此代码,所以如果出现任何问题,请告诉我。
.index()
为您获取匹配元素的索引。
请注意,您不希望在不更改克隆中元素的id的情况下对其进行更改——id
在整个页面上应该是唯一的。我还没有删除片段中的所有内容。
function fixVisibility()
{
// or just do this in css
if ($(".clonedInput").length == 1) {
$('.remove').hide();
} else {
$('.remove').show();
}
}
function fixIndices()
{
// if you delete an item on top, the indices should be fixed.
$('.clonedInput').each(function(){
var tr = $(this);
var cloneIndex = tr.index() + 1;
tr.removeAttr('id')
.attr("id", "clonedInput" + cloneIndex)
.find(".heading-reference").text('Set ' + cloneIndex).end();
});
}
function clone()
{
var tr = $(this).closest(".clonedInput");
var cloneIndex = tr.index() + 2;
tr.clone().removeAttr('id')
.attr("id", "clonedInput" + cloneIndex)
.find(".heading-reference").text('Set ' + cloneIndex).end()
.appendTo('.tbodyClone');
fixVisibility();
//delete
console.log("Total lines => " + $(".clonedInput").length);
}
function remove() {
$(this).parents(".clonedInput").remove();
fixVisibility();
fixIndices();
}
fixVisibility();
$(document).on("click", ".clone", clone);
$(document).on("click", ".remove", remove);
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="form-group">
<tbody class="tbodyClone">
<tr id="clonedInput1" class="clonedInput">
<td><h4 name="set" class="heading-reference">Set 1</h4>
<select id="style" class="form-control">
<option>Pull ups</option>
<option>Push ups</option>
</select></td>
<td><select id="weight" class="form-control">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select></td>
<td><select id="reps" class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
</select></td>
<td>
<button class="clone btn btn-success"><i class="fa fa-plus-circle"></i></button>
<button class="remove btn btn-danger"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
</tbody>
</table>
相关文章:
- Javascript更新孙窗口中的表单元素
- 阻止表单元素提交
- 使用带有.net autopostback的ryanfait.com自定义表单元素
- 使用javascript更改表单元素的ID值
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 为什么表单元素不应命名为submit
- 如何重置搜索表单中的特定表单元素
- 使用 ng-repeat访问 ng 表单元素/值
- 获取电子邮件附件的表单元素
- AJAX和php脚本后添加的表单元素未传输到$_POST
- 如何对未知表单元素进行表单验证
- 不显示为表单元素的图像输入按钮的动态创建和appendChild
- 在JavaScript中为表单元素生成键值对
- 表单元素上的jQuery focusout事件即使在单击子元素时也会被触发
- 如何在 OnKeyPress 事件后获取输入表单元素的值
- 表单元素值更改后的角度 JS 验证
- 使用 javascript 中的表单元素构建一个 xml 字符串
- 如何以角度访问表单元素的$valid
- 如果选择了多个选项,则使用 jQuery 显示隐藏的表单元素
- JSON 填充的表单元素不使用 angularJs 过滤器进行过滤