动态删除没有唯一属性的输入字段 [英] Deleting input fields dynamically that dont have a unique attribute

查看:53
本文介绍了动态删除没有唯一属性的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态表单,该表单允许在jquery的帮助下动态添加和删除字段.现有字段是从mysql表中的值自动填充的. add button添加一个新的输入字段,而delete button删除一个输入字段.从数据库加载的字段的字段标记为data-saved.现在,我的困境集中在delete button上.如何删除未使用data-saved属性标记的新部分? 示例

I have a dynamic form that allows fields to be added and deleted dynamically with the help of jquery. The existing fields are autopopulated from values in mysql table. The add button adds a new input field while the delete button removes an input field. The fields loaded with values from the db are tagged with data-saved attributed. Now my dilemma is focused in the delete button. How can I delete new sections that are not tagged with data-saved attribute? 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/

这篇关于动态删除没有唯一属性的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆