javascript - jQuery在克隆后更改div的子元素的所有名称

标签 javascript jquery clone

因此,我有以下 jquery 代码,当某个字段中的输入值增加时,它会克隆一个元素。

$(document).ready(function(){
    $("#nmovimentos").change(function () {
        var direction = this.defaultValue < this.value
        this.defaultValue = this.value;
        if (direction)
        {
                var $div = $('div[id^="novomov"]:last');
                var num = parseInt( $div.prop("id").match(/\d+/g), 10 ) +1;
                var $clone = $div.clone().prop('id', 'novomov'+ num)
                $clone.insertAfter('[id^="novomov"]:last');
        }
        else $('[id^="novomov"]:last').remove();
    });
});

但是,它克隆了一个 div,其中包含带有大量输入字段的表单的一部分。

<div id="novomov1" class="novomov">
<table id="tab">
<tr name="linear1" id="linear1">
        <td>
            Cardinalidade:<input type="text" name="card1" id="card1" value=""><br>
            Angulo 1:<input type="text" name="param1" id="angulo1" value=""><br>
            Angulo 2:<input type="text" name="param2" id="angulo2" value=""><br>
            Tamanho:<input type="text" name="param3" id="tamanho1" value=""><br>
            Descricao:<input type="text" name="descricao1" id="descricao1" value=""><br>
            Tempo:<input type="text" name="tempo1" id="tempo1" value=""><br>
        </td></tr></table></div>

我需要更改所有克隆 div 后代的名称,以便将这些参数传递到数据库。我考虑使用 jquery 函数中的 var num 将名称增加 1。不过我有点迷失了..所以,有什么关于如何做到这一点的线索吗?非常感谢!

最佳答案

代码已更改,以检索克隆 div 内的所有输入并更改其名称/id。

<script>    
    $(document).ready(function(){
            $("#nmovimentos").change(function () {
                var direction = this.defaultValue < this.value
                this.defaultValue = this.value;
                if (direction)
                {
                        var $div = $('div[id^="novomov"]:last');
                        var num = parseInt( $div.prop("id").match(/\d+/g), 10 ) +1;
                        var $clone = $div.clone().prop('id', 'novomov'+ num)
                        $clone.insertAfter('[id^="novomov"]:last');
                        // get all the inputs inside the clone
                        var inputs = $clone.find('input');
                        // for each input change its name/id appending the num value
                        $.each(inputs, function(index, elem){
                            var jElem = $(elem); // jQuery element
                            var name = jElem.prop('name');
                            // remove the number
                            name = name.replace(/\d+/g, '');
                            name += num;
                            jElem.prop('id', name);
                            jElem.prop('name', name);
                        });
                }
                else $('[id^="novomov"]:last').remove();
            });
        });
    </script>

关于javascript - jQuery在克隆后更改div的子元素的所有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677068/

相关文章:

javascript - 如何仅在 ES2015 中生成从 0 到 n 的数字范围?

javascript - 如何使用 Context API 将单个 Socket IO 实例传递给页面?

jQuery 循环多种形式

jquery - Spring Boot Ajax post表单使用模型和thymeleaf提交

jQuery 滚动、事件菜单突出显示

javascript - jQuery clone() 多个列表并将每个列表附加到其原始列表

javascript - 如何在jade中访问js数组的数据

javascript - 使用 Google Closure 编译器时在浏览器和 node.js 之间共享 JS 的最佳方式

apache - 阻止 "cloner"服务器从我们的服务器渲染内容

wordpress - 如何防止抓取我博客的更新?