You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何扩展JavaScript修改嵌套div中子节点的input元素名称?

Fixing Nested Input Name Updates for Bootstrap Form Fields

Got it, let's break down why your script stopped working once you added those Bootstrap column divs. The original code only looks at the direct child nodes of the cloned keyApp container—but now your inputs are tucked inside extra <div class="col-md-2"> elements. That means the loop never actually reaches the inputs to update their names. Here's how to fix this, with two solid options:

Option 1: Use querySelectorAll (Simplest & Most Efficient)

This method directly targets all <input> elements inside the cloned div, regardless of how deeply they're nested. Perfect for Bootstrap's column structure:

var appCounter = 0;
function anotherApp() {
    appCounter++;
    var newAppField = document.getElementById("keyApp").cloneNode(true);
    newAppField.id = '';
    newAppField.style.display = 'block';
    
    // Grab ALL input elements inside the cloned container
    var inputs = newAppField.querySelectorAll('input');
    for (var i = 0; i < inputs.length; i++) {
        var theName = inputs[i].name;
        if (theName) {
            inputs[i].name = theName + appCounter;
            // Optional: Update IDs too (since page IDs must be unique)
            inputs[i].id = theName + appCounter;
        }
    }
    
    var insertApp = document.getElementById('keyApp');
    insertApp.parentNode.insertBefore(newAppField, insertApp);
    document.getElementById('appCount').value = appCounter;
}

Why this works:

querySelectorAll('input') scans every level of the cloned div's DOM tree to find inputs, so it doesn't matter if they're wrapped in Bootstrap columns or other containers. I also added an optional line to update the input IDs—this is a good practice because HTML requires all IDs to be unique on a page.

Option 2: Recursive Node Traversal (For Complex Nested Structures)

If you ever have even deeper nested elements (like inputs inside tabs, accordions, etc.), a recursive function will traverse every node in the cloned container to find inputs:

var appCounter = 0;
function anotherApp() {
    appCounter++;
    var newAppField = document.getElementById("keyApp").cloneNode(true);
    newAppField.id = '';
    newAppField.style.display = 'block';
    
    // Recursive function to check every node for inputs
    function updateInputNames(node) {
        // Check if this node is an input with a name attribute
        if (node.tagName === 'INPUT' && node.name) {
            node.name = node.name + appCounter;
            node.id = node.name; // Sync ID with updated name
        }
        // Recurse through all child nodes
        for (var i = 0; i < node.childNodes.length; i++) {
            updateInputNames(node.childNodes[i]);
        }
    }
    
    // Start traversing from the cloned div
    updateInputNames(newAppField);
    
    var insertApp = document.getElementById('keyApp');
    insertApp.parentNode.insertBefore(newAppField, insertApp);
    document.getElementById('appCount').value = appCounter;
}

Why this works:

This function digs through every single child node (and their children) of the cloned container, ensuring no input gets missed—even if your form structure gets more complex later on.

Both solutions will work with your updated Bootstrap form structure. The first option is ideal for your current setup, while the second gives you flexibility for future changes.

内容的提问来源于stack exchange,提问作者PGHE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:17:25