我有一個隱藏的html表格tr。這個想法是通過點擊一個buttton來添加更多trs。克隆html TR不克隆所有單元值
<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爲空。但是,當我再次單擊$(「#adicionar」)按鈕時,將出現第二個tr,並添加了第一個tr的值。
當您提示'$(「。fila-base input#precioUnitario」).val()'時,您是否看到你期望的價值? – Marc
是的,我看到了價值。 –
克隆具有'id'屬性的元素時請小心。它們在文檔中應該是唯一的。 – Andrew