gpt4 book ai didi

javascript - 动态删除不具有唯一属性的输入字段

转载 作者:行者123 更新时间:2023-11-30 17:44:06 28 4
gpt4 key购买 nike

我有一个动态表单,允许在 jquery 的帮助下动态添加和删除字段。现有字段是根据 mysql 表中的值自动填充的。 add 按钮 添加一个新的输入字段,而 delete 按钮 删除一个输入字段。从数据库中加载值的字段被标记为 data-saved 属性。现在我的难题集中在 delete 按钮 上。如何删除未标记为 data-saved 属性的新部分? EXAMPLE

JQUERY

$(document).ready(function () {
$('#btnAdd').click(function () {
var $clones = $('#input_table tr'),
num = $clones.size() + 1,
next_num = parseInt($clones.last().find('input[name^="person_id"]').val()) + 1,
$template = $clones.first(),
newSection = $template.clone().attr('id', 'pq_entry_'+num),
person_id = 'person_id_'+num;
person_fname = 'person_fname_'+num;
person_status = 'person_status_'+num;

newSection.removeAttr('data-saved');

// clear out all sections of new input
newSection.find('input').val('');
newSection.find('select').val([]);

newSection.find('input[name^="person_id"]').attr({
'id': person_id,
'name': person_id
}).val();
newSection.find('input[name^="person_fname"]').attr({
'id': person_fname,
'name': person_fname,
'placeholder' : 'Person #'+num+' First Name'
}).val();
newSection.find('select').attr({
'id': person_status,
'name': person_status
}).val(next_num);
newSection.find('input[type="button"]').attr('data-ident', next_num);


$('#input_table').append(newSection);
$('#btnDel').prop('disabled', '');
if (num == 100) $('#btnAdd').prop('disabled', 'disabled');
});

$('#btnDel').click(function () {
var num = $('.clonedSection').length; // how many duplicate input fields we currently have
$('#pq_entry_' + num).remove(); // remove the last element

// enable the "add" button
$('#btnAdd').prop('disabled', '');

// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#btnDel').prop('disabled', 'disabled');

});

$('#btnDel').prop('disabled', 'disabled');
});

HTML

<tbody id="input_table" >
<tr id="pq_entry_1" class="clonedSection" data-saved="1">
<td><input type="text" name="person_id" value='1' readonly /></td>
<td>
<input id="person_id_1" name="person_id_1" type="text" value='1'/>
<input id="person_fname_1" name="person_fname" placeholder=" First Name" type="text" value='James'/>
</td>
<td>
<select id="person_status_1" name="person_status_1"></select>
</td>
</tr>
<tr id="pq_entry_2" class="clonedSection" data-saved="2">
<td><input type="text" name="person_id" value='2' readonly /></td>
<td>
<input id="person_id_2" name="person_id_2" type="text" value='2'/><input id="person_fname_2" name="person_fname" placeholder=" First Name" type="text" value='Cynthia'/>
</td>
<td>
<select id="person_status_2" name="person_status_2"></select>
</td>
</tr>
</tbody>
<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='Delete New Field' /></br>

最佳答案

来自

$('#pq_entry_' + num).remove(); // remove the last element

变成

var toDelete = $('#pq_entry_' + num).not('[data-saved]');

if (toDelete.length) {
// Last one wasn't a db-entry
toDelete.remove();

// enable the "add" button
$('#btnAdd').prop('disabled', '');

// if only one element remains, disable the "remove" button
if ($('.clonedSection:not([data-saved])').length == 0)
$('#btnDel').prop('disabled', 'disabled');
}

工作示例:http://jsfiddle.net/az9LQ/

关于javascript - 动态删除不具有唯一属性的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447448/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com