我想用默认值克隆 clonenode()

I want to clone with default values clonenode()

我想做的是在table中克隆一行。这里的代码:

function addRow(){ 
    var x=document.getElementById("insertar").tBodies[0];  //get the table
    var node=x.rows[1].cloneNode(true);    //clone the previous node or row
    x.appendChild(node);   //add the node or row to the table
}

我只是不想克隆其中的这一段,因为当我往第一行添加数据的时候,你克隆的时候,数据也被克隆了。 我拍了HTML代码:

<table id='insertar'>
                <tr><td>Producto</td><td>Stock</td><td>Cantidad</td></tr>
                <?php 
                $numeroItems = 1;
                try{
                    $prod = array();
                    $param = array('TIPOP'=> 'Productos');
                    $ready = $client->MOSTRARPRODUCTOS($param)->MOSTRARPRODUCTOSRESULT;
                    array_push($prod,$ready->PRODUCTOS);
                }catch(Exception $e){
                    echo $e->getMessage();
                }
                echo "
                <tr><td>
                <select id='p' name='producto[]' onChange='document.getElementById(\"stock\").value = setStock()' >";
                echo "<option value='' >---------</option>";
                if(count($ready->PRODUCTOS)>1){
                    for($j = 0;$j < (count($ready->PRODUCTOS)); $j++){
                        if($ready->PRODUCTOS[$j]->GRUPO==$_GET['grupo']){
                            echo "<option value=".trim($ready->PRODUCTOS[$j]->CODIGOP)." >".trim($ready->PRODUCTOS[$j]->CODIGOP)."</option>";
                        }
                    }
                }
                echo "
                </select><br />
                </td>
                <td><input type='text' id='stock' value='' name='stock[]' readonly></input></td>
                <td><input type='text' name='cantidad[]' required></input></td></tr>";
                ?>
            </table>
            <a href="#" onclick="addRow()" >Agregar otro</a>

现在我想更改属性 td 以便它们是唯一的,您会怎么做? 克隆示例:

<tr><td>.....</td><td id="stock1" ...></td></tr>
<tr><td>.....</td><td id="stock2" ...></td></tr>
<tr><td>.....</td><td id="stock3" ...></td></tr>
Etc...

您应该在默认节点被修改之前克隆它。然后根据需要在 addRow.

中重新克隆 default
var _default;
window.onload = function(){
  _default = document.getElementById('defaultNode').cloneNode(true);
}

像这样:

function addRow(){ 
    var x=document.getElementById("insertar").tBodies[0];  //get the table
    x.appendChild(_default);   //add the node or row to the table
}

您建议的工作解决方案:

var _default;
window.onload = function() {
  var x = document.getElementById("insertar").tBodies[0];
  _default = x.rows[1].cloneNode(true);
}

编辑2:

var counter = 1;
function addRow(){ 
    var _cloned_default = _default.cloneNode(true);
    _cloned_default.id = "stock" + counter;
    counter++;
    x.appendChild(_cloned_default);   //add the node or row to the table
}

只需在克隆的元素中获取 td 元素即可:

Array.prototype.slice.call(_cloned_node.childNodes, 0).forEach(function(value){
    //making sure it's a node and nothing else
    if(value.nodeType === 1){
        //change id here
        value.id = counter;
        counter++;
  }
});