Repetindo valor em input dinamico
Boa noite,
tenho um script que adiciona dinamicamente alguns campos. Mas queria ele repetisse o valor que eu jogasse no segundo campo quando eu adicionasse um novo.
Segue meu script abaixo. Se alguem conseguir me ajudar, agradeço desde já:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
//atente-se aos seletores ( class="p" ,class="linhas") to pegando seletor pela class
//
//Add campos para parcela
$(".addP").click(function () {
novoCampo = $("tr.p:first").clone();
novoCampo.find("input").val("");
novoCampo.insertAfter("tr.p:last");
removeCampoP();
});
});
function removeCampoP() {
$(".removerCampo").unbind("click");
$(".removerCampo").bind("click", function () {
if ($("tr.p").length > 1) {
$(this).parent().parent().remove();
}
});
}
function removeCamp() {
$(".removerCampo").unbind("click");
$(".removerCampo").bind("click", function () {
if ($("tr.linhas").length > 1) {
$(this).parent().parent().remove();
}
});
}
//fim
//add inputs para produtos
$(".adicionarCampo").click(function () {
novoCamp = $("tr.linhas:first").clone();
novoCamp.find("input").on('keyup', function () {
calcula(); //faz multiplicação
}).attr('class', 'txt').val("");
novoCamp.insertAfter("tr.linhas:last");
removeCamp();
});
function removeCampo() {
$(".removerCampo").unbind("click");
$(".removerCampo").bind("click", function () {
if ($("tr.linhas").length > 1) {
$(this).parent().parent().remove();
}
});
}
//fim dos produtos
function calcula() {
var sum = 0;
$(".linhas").each(function () {
var v1 = parseInt(1),
v2 = $(this).find('input')[1];
if (!isNaN(v2.value) && v1 != 0 && !isNaN(v1) && v2.value.length != 0) {
sum += parseFloat(v1 * v2.value);
}
});
$("#sum").html(sum.toFixed(2)); //faz a saida
}
</script>
</head>
<body>
<form action='action_cadastrar.php' method="post" name='cadastro'>
<a href="#" class="addP">ADICIONAR PARCELA</a>
<table>
<th class='coluna'>
<input type="text" name="data_vencimento[]" maxlength='10' OnKeyPress="formatar(this, '##/##/####')" onClick="this.value=''"/>
</th>
<th class='coluna'>
R$ <input type="text" name="valor_parcela[]" size='8' >
</th>
<th class='coluna'>
<input type="text" name="codigo_barra[]" size='40'>
<a href="#" class="removerCampo"> x</a></th>
</tr>
<tr class='coluna'>
<th>
</table>
</body>
</body>
</html>Discussão (1)
Carregando comentários...