克隆html TR并未克隆所有单元格值

时间:2015-10-30 00:08:55

标签: javascript jquery html

我有一个带有隐藏 tr 的html表。想法是通过点击一个按钮来添加更多 trs

        <tr class="fila-base hidden">
            <td><input class="form-control input-sm col-xs-1" name="cantidad" id="cantidad" value="0"/></td>
            <td><input type="text" class="form-control input-sm col-xs-2" name="codigoPrincipalProducto" id="codigoPrincipalProducto"/></td>
            <td><input type="text" class="form-control input-sm col-xs-2" name="codigoAuxiliarProducto" id="codigoAuxiliarProducto"/></td>
            <td><input type="text" class="form-control input-sm col-xs-1" name="precioUnitario" id="precioUnitario"/></td>
            <td><input type="number" class="form-control input-sm col-xs-1" name="tDescuento" value="0"/></td>
            <td><input type="number" class="form-control input-sm col-xs-1 " name="precioTotal"/></td>
            <td><input type="number" class="form-control input-sm col-xs-1" name="tIce" id="tIce" value="0"/></td>
            <td class="hidden"><input type="number" class="form-control input-sm col-xs-1" name="tidVP" id="tidVP" value="0"/></td>
            <td id="eliminar-fila"><i class="btn btn-primary btn-xs"><label class="glyphicon glyphicon-trash"></label> Eliminar</i></td>
        </tr>

我有一个javascript代码,通过AJAX填充所有输入。我想在为每个输入设置值后克隆隐藏的 tr

            $("#adicionar").on('click', function(event){
            var codP = $("#codP").val();
            var codA = $("#codA").val();
            event.preventDefault();

                $.ajax({
                    url : window.location.pathname + "/precioProducto",
                    type : "POST",
                    contentType : 'application/json',
                    mimeType: 'application/json',
                    dataType : 'json',
                    data :  {codP : codP, codA : codA},

                    success : function(response) {
                        $(".fila-base input#precioUnitario").val(response);
                        alert ($(".fila-base input#precioUnitario").val());
                    },
                    error : function(xhr, status, error) {
                        alert("No se encontró en precio del producto agregado");
                    }
                });

            $(".fila-base input#codigoPrincipalProducto").val(codP);
            $(".fila-base input#codigoAuxiliarProducto").val(codA);

            $("#tablaVenta tbody tr:eq(0)").clone().removeClass('fila-base hidden').appendTo("#tablaVenta tbody");

        });

问题在于,当我克隆隐藏的 tr 时,除了 precioUnitario 之外,其所有输入都被克隆。这意味着我可以看到包含所有值的tr,输入 precioUnitario 为空。但是,当我再次点击按钮$(&#34; #adicionar&#34;)时,会出现第二个 tr ,其中包含我添加的第一个 tr 的值。< / p>

1 个答案:

答案 0 :(得分:2)

您在#precioUnitario - 回调中设置了$.ajax的值,因此当您想要确保该值已经存在时,您还必须将克隆的<tr>附加到ajax回调中已克隆<tr>

时已设置

将克隆其他输入的值,因为它们的值不会在回调中设置