javascript - 同时动态创建多个输入

标签 javascript jquery html input

我正在制作一个表单,其中一个字段集将包含用户根据需要添加的输入字段,该输入字段将具有可选择的高度和可选的复选框。我找到了一个很好的例子,说明如何一次添加一种输入类型,但不能同时添加和配对在一起。对此有任何想法将不胜感激!谢谢!你们都是我的英雄。 单个输入代码的示例可以在此处找到 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');
        });

最佳答案

您拥有的代码已经克隆了 .clonedInput div 中的内容。因此,您所需要做的就是在文本框后面添加一个复选框。

<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>

要获取 id/name 值以附加新数字,您需要调整此行,这将为文本框元素提供 ID 和 name1、name2、name3 等名称:

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

并添加此行,这将为 checkbox 元素提供 chk1、chk2、chk3 等 ID 和名称:

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

这是一个updated fiddle .

关于javascript - 同时动态创建多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20961101/

相关文章:

javascript - 窗口大小更改时 jQuery 不执行

javascript - 无法使用下拉菜单作为表单输入

javascript - 使用 JavaScript 插入 HTML Img 标签时遇到问题

html - 如何保持2个固定div的相同位置?

JavaScript:RIA 工具包

javascript - 当登录状态处于组件状态时,如何使用 react-router 实现依赖于登录的路径?

javascript - 如何编写插件或用户脚本将嵌入的 YouTube 视频弹出到新窗口?

javascript - 电子邮件被多次发送

javascript - 搜索时删除前置用户

javascript - 发布在 Ajax 中无法将输入保存到数据库