jQuery动态形式的单个div中的多个文本字段 [英] Multiple text fields in single div with jQuery dynamic form

查看:97
本文介绍了jQuery动态形式的单个div中的多个文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此处,但是我试图在每个div中输入多个文本.它会正确显示并递增并保存第一个元素的名称和ID,但对于第二个年龄输入,它只会存储第一个输入.

Using the jQuery code from here, but I am trying to have multiple text inputs in each div. It displays correctly and increments and saves the name and id for the first element, but for the second age input, it only stores the first input.

 <html>
    <head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
<script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);

                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

                newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
                $('#input' + num).after(newElem);
                $('#btnDel').attr('disabled','');

                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

            $('#btnDel').click(function() {
                var num = $('.clonedInput').length;

                $('#input' + num).remove();
                $('#btnAdd').attr('disabled','');

                if (num-1 == 1)
                    $('#btnDel').attr('disabled','disabled');
            });

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

<body>

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

</body>
</html>

这是我的新表单和jQuery代码的样子.

Here is what my new form and jQuery code looks like.

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

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

newElem.children(':first')
             .attr('id', 'age' + newNum)
            .attr('name', 'age' + newNum);

推荐答案

您的脚本仅更新第一个输入,您需要遍历并更新克隆的div中的所有输入

Your script only updates the first input, you need to loop through and update all inputs in the cloned div

$('#btnAdd').click(function () {
    var num = $('.clonedInput').length;
    var newNum = num + 1;

    var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

    newElem.find(':input').each(function () {
        $(this).attr('id', $(this).attr('id').replace(/\d+/, newNum));
        $(this).attr('name', $(this).attr('name').replace(/\d+/, newNum));
    });

    $('#input' + num).after(newElem);
    $('#btnDel').attr('disabled', '');

    if (newNum == 5) $('#btnAdd').attr('disabled', 'disabled');
});

这篇关于jQuery动态形式的单个div中的多个文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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