【发布时间】:2016-10-18 12:16:01
【问题描述】:
我有一个表单,其中单个 id entry1 中有 6 个输入字段。当我单击 Add Section 按钮时,这些字段被克隆并添加到表单中。 删除上面的部分有一个按钮。
当我点击上面的删除部分时,上面的 6 个输入字段被删除。但我想在每个克隆的表单字段中删除一个按钮。当我单击删除时,仅删除该特定部分。目前我在下面的表格中只有一个删除部分按钮。 这是我的 html 表单。
<div class="col-md-12">
<div id="entry1" class="clonedInput col-md-12">
<h5 id="reference" name="reference" class="heading-reference">Entry #1</h5>
<div class="col-md-2">
<div class="form-group">
<label class="label_item" for="ID_1_item">Item</label>
<input type="text" class="input_item form-control" name="ID_1_item" id="ID_1_item">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label class="label_place" for="ID_1_place">Place</label>
<input class="input_place form-control" type="text" name="ID_1_place" id="ID_1_place" value="">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label class="label_slip_no" for="ID_1_slip_no">BCMS Test Slip No.</label>
<input class="input_slip_no form-control" type="text" name="ID_1_slip_no" id="ID_1_slip_no" value="">
</div>
</div>
<div class="col-md-2">
<label class="label_result" for="ID_1_result">Result</label>
<select class="input_result form-control" name="ID_1_result" id="ID_1_result">
<option value="OK">OK</option>
<option value="Failed">Failed</option>
</select>
</div>
<div class="col-md-2">
<label class="label_reason" for="ID_1_reason">Reason</label>
<textarea class="input_reason form-control" type="text" name="ID_1_reason" id="ID_1_reason" ></textarea>
</div>
<div class="col-md-2">
<label class="label_pdf" for="ID_1_pdf">Upload Report</label>
<input class="form-control input_pdf" type = "file" name = "ID_1_pdf" id="ID_1_pdf" size = "20" required="" />
</div>
</div><!-- end #entry1 -->
<div id="addDelButtons col-md-12">
<input class="btn btn-success" type="button" id="btnAdd" value="add section"> <input class="btn btn-danger" type="button" id="btnDel" value="remove section above">
</div>
</div>
<!-- /.row -->
</div>
<div class="box-footer">
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</div>
我的 id entry1 克隆的 Jquery。
<script type="text/javascript">
$(function () {
$('#btnAdd').click(function () {
var num = $('.clonedInput').length, // how many "duplicatable" input fields we currently have
newNum = new Number(num + 1), // the numeric ID of the new input field being added
newElem = $('#entry' + num).clone().attr('id', 'entry' + newNum).fadeIn('slow'); // create the new element via clone(), and manipulate it's ID using newNum value
// manipulate the name/id values of the input inside the new element
// H2 - section
newElem.find('.heading-reference').attr('id', 'ID' + newNum + '_reference').attr('name', 'ID' + newNum + '_reference').html('Entry #' + newNum);
// Title - select
newElem.find('.label_item').attr('for', 'ID_' + newNum + '_item');
newElem.find('.input_item').attr('id', 'ID_' + newNum + '_item').attr('name', 'ID_' + newNum + '_item').val('');
// First name - text
newElem.find('.label_place').attr('for', 'ID_' + newNum + '_place');
newElem.find('.input_place').attr('id', 'ID_' + newNum + '_place').attr('name', 'ID_' + newNum + '_place').val('');
// Last name - text
newElem.find('.label_slip_no').attr('for', 'ID_' + newNum + '_slip_no');
newElem.find('.input_slip_no').attr('id', 'ID_' + newNum + '_slip_no').attr('name', 'ID_' + newNum + '_slip_no').val('');
// Color - checkbox
newElem.find('.label_result').attr('for', 'ID_' + newNum + '_result');
newElem.find('.input_result').attr('id', 'ID_' + newNum + '_result').attr('name', 'ID_' + newNum + '_result').val([]);
// Skate - radio
newElem.find('.label_reason').attr('for', 'ID_' + newNum + '_reason');
newElem.find('.input_reason').attr('id', 'ID_' + newNum + '_reason').attr('name', 'ID_' + newNum + '_reason').val([]);
// Skate - radio
newElem.find('.label_pdf').attr('for', 'ID_' + newNum + '_pdf');
newElem.find('.input_pdf').attr('id', 'ID_' + newNum + '_pdf').attr('name', 'ID_' + newNum + '_pdf').val([]);
// insert the new element after the last "duplicatable" input field
$('#entry' + num).after(newElem);
$('#ID' + newNum + '_title').focus();
// enable the "remove" button
$('#btnDel').attr('disabled', false);
// right now you can only add 5 sections. change '5' below to the max number of times the form can be duplicated
var test = document.getElementById('tstquantity').value;
if (newNum == test)
$('#btnAdd').attr('disabled', true).prop('value', "You've reached the limit");
});
$('#btnDel').click(function () {
// confirmation
if (confirm("Are you sure you wish to remove this section? This cannot be undone."))
{
var num = $('.clonedInput').length;
// how many "duplicatable" input fields we currently have
$('#entry' + num).slideUp('slow', function () {$(this).remove();
// if only one element remains, disable the "remove" button
if (num -1 === 1)
$('#btnDel').attr('disabled', true);
// enable the "add" button
$('#btnAdd').attr('disabled', false).prop('value', "add section");});
}
return false;
// remove the last element
// enable the "add" button
$('#btnAdd').attr('disabled', false);
});
$('#btnDel').attr('disabled', true);
});
</script>
【问题讨论】:
-
请提供最小的复制品,这样想要帮助您的人就不必阅读所有 Bootstrap HTML。它还可以更轻松地找到解决方案。
-
这个
but I want to a remove option to the after every 6 form fields是什么意思?? -
这意味着当我单击“添加部分”按钮时,克隆的每个表单字段都在 entry1 id 中。我还想要一个“删除部分”按钮,通过它我可以删除特定的克隆字段。
-
那么,您需要为每个字段添加一个“删除”按钮吗?'
-
不是每个字段。有一个 id entry1。当我单击添加部分时,entry1 克隆到 entry2、entry2、entry4 等,其中还克隆了 6 个表单字段。我需要为每个克隆的 id entry2、entry3、entry4 等提供一个删除按钮。..
标签: javascript jquery html clone