【问题标题】:jQuery - Adding / Removing Input Fields in FormjQuery - 在表单中添加/删除输入字段
【发布时间】:2013-09-06 04:09:19
【问题描述】:

我已经搜索过,但在这里找不到解决问题的答案。我对 jQuery 相当陌生,我希望从两个不同的区域添加/删除输入字段。我已经尝试编辑我的 jQuery 和 HTML 代码以适应我想要添加/删除的第二部分(找到的项目),但我无法让它工作。任何帮助将不胜感激!

jQuery:

<script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd2').click(function() {
                var num2        = $('.clonedInput2').length;    //     how many "duplicatable" input fields we currently have
                var newNum  = new Number(num2 + 1);     // the numeric ID of the new input field being added

            // create the new element via clone(), and manipulate it's ID using newNum value
            var newElem2 = $('#input2' + num2).clone().attr('id', 'input2' + newNum);

            // manipulate the name/id values of the input inside the new element
            newElem2.children(':first').attr('id', 'name' + newNum).val(null);

            // insert the new element after the last "duplicatable" input field
            $('#input2' + num2).after(newElem2);

            // enable the "remove" button
            $('#btnDel2').attr('disabled','');

        });

        $('#btnDel2').click(function() {
            var num = $('.clonedInput2').length;    // how many "duplicatable" input fields we currently have
            $('#input2' + num).remove();        // remove the last element

            // enable the "add" button
            $('#btnAdd2').attr('disabled','');

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

        $('#btnDel2').attr('disabled','disabled');
    });
</script>

HTML:

<form id="myForm" action="process_call.php" method="post">
        <div id="input1" style="margin-bottom:4px;" class="clonedInput">
            Charge: <input type="text" name="name[]" id="name1" />
        </div>
        <div>
            <input type="button" id="btnAdd" value="Add Another Charge" />
            <input type="button" id="btnDel" value="Remove Charge" />
        </div>

        <div id="input2" style="margin-bottom:4px;" class="clonedInput2">
            Item Found: <input type="text" name="item[]" id="item1" />
        </div>
        <div>
            <input type="button" id="btnAdd2" value="Add Another Item" />
            <input type="button" id="btnDel2" value="Remove Item" />
        </div>
        <input type="submit">
    </form>

【问题讨论】:

    标签: jquery forms dynamic input


    【解决方案1】:

    问题是选择器$('#input2' + num2),当脚本第一次执行时,只有一个id为input2的元素,但你的选择器正在寻找一个id为input21的元素,它不会退出。

    我通过克隆具有类 clonedInput2 的最后一个元素而不是查找具有 id 的元素来修复它

    jQuery(function($) {
        $('#btnAdd2').click(function () {
            var num2 = $('.clonedInput2').length; //     how many "duplicatable" input fields we currently have
            var newNum = num2 + 1; // the numeric ID of the new input field being added
    
            // create the new element via clone(), and manipulate it's ID using newNum value
            var newElem2 = $('.clonedInput2:last').clone().attr('id', 'input2' + newNum);
    
            // manipulate the name/id values of the input inside the new element
            newElem2.children(':first').attr('id', 'name' + newNum).val(null);
    
            // insert the new element after the last "duplicatable" input field
            $('.clonedInput2:last').after(newElem2);
    
            // enable the "remove" button
            $('#btnDel2').prop('disabled', false);
    
        });
    
        $('#btnDel2').click(function () {
            var num = $('.clonedInput2').length; // how many "duplicatable" input fields we currently have
            $('#input2' + num).remove(); // remove the last element
    
            // enable the "add" button
            $('#btnAdd2').attr('disabled', '');
    
            // if only one element remains, disable the "remove" button
            if (num - 1 == 1) $('#btnDel2').attr('disabled', 'disabled');
        });
    
        $('#btnDel2').attr('disabled', 'disabled');
    });
    

    演示:Fiddle

    【讨论】:

    • 非常感谢!我不得不进行一些小的编辑来修复删除按钮不适用于您的代码,但效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多