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

标签 javascript jquery

我有一个动态表单,允许在 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/

相关文章:

javascript - Select2 与 collectionType - Symfony2

Javascript 用文本变量替换预定义的文本

jquery - 如何使用 Rails 3 更新到 jQuery 1.7+?

jquery - css,翻转后无法点击背面

javascript - 谷歌翻译 : Callback when a language is selected

javascript - 在 JavaScript 最佳实践中组织库依赖项

javascript - require() 在 node.js 中需要相同模块时如何工作

jquery - jquery .click 和 .css 的清洁方法

jquery - 如何在 H3 元素内对齐文本和图像

jquery - 仅验证字段字母 jquery/ajax