简体   繁体   中英

Dynamically creating multiple inputs simultaneously

I'm making a form in which one field set will have input fields added by the user as necessary that will have a selectable height and an optional check-box. I found a good example on how to add one input type at a time but not both simultaneously and paired together. Any ideas would be appreciated on this one! Thanks! Y'all are my hero. The example for a single input code can be found here http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding-form-elements/

HTML:

<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Name: <input type="text" name="name1" id="name1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
        <input type="button" id="btnDel" value="remove name" />
    </div>
</form>

JS:

$(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var newNum  = new Number(num + 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 newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

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

                // insert the new element after the last "duplicatable" input field
                $('#input' + num).after(newElem);

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

                // business rule: you can only add 5 names
                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

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

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

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

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

The code you have will already clone what ever is inside of the .clonedInput div. So all you need to do is add a checkbox after the textbox.

<div id="input1" style="margin-bottom:4px;" class="clonedInput">
    Name: <input type="text" name="name1" id="name1" /> <input type="checkbox" name="chk1" id="chk1" />
</div>

To get the id/name values to append a new number you'll need to adjust this line, which will give the textbox elements an ID and Name of name1, name2, name3, etc.:

newElem.children('input[type=text]:first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);

and add this line, which will give the checkbox elements an ID and Name of chk1, chk2, chk3, etc.:

newElem.children('input[type=checkbox]:first').attr('id', 'chk' + newNum).attr('name', 'chk' + newNum);

Here is an updated fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM