JSFiddle - React, Tailwind, and code Playground
by andredgusmao
HTML
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<button id="botao">Adiciona linha</button>
<input type="hidden" id="data" data-id="1" data-name="produto" data-price="19.99"/>
<table>
<thead>
<tr><td>Input</td></tr>
</thead>
<tbody id="products_table">
<tr>
<td><input type="text" class="dinheiro"/></td>
</tr>
</tbody>
</table>
JavaScript
$(document).ready( function () {
$(".dinheiro").maskMoney();
$('#botao').on('click', function () {
addProd($('#data'));
});
function addProd(obj) {
$('#add_prod').val('');
var id = $(obj).attr('data-id');
var name = $(obj).attr('data-name');
var price = $(obj).attr('data-price');
$('.searchresults').hide();
console.log($('input[name="quant[' + id + ']"]').length);
if ($('input[name="quant[' + id + ']"]').length == 0) {
var tr =
'<tr>' +
'<td>' + id + ' - ' + name + '</td>' +
'<td>' +
'<input type="number" name="quant" class="p_quant" value="1" onchange="updateSubtotal(this)" data-price="' + price + '" />' +
'</td>' +
'<td>' + price + '</td>' +
'<td>' +
'<input type="text" name="servico[' + id + ']" class="servico" onkeyup="carregaServico(this)" id="servico[' + id + ']" data-type="search_servico" />' +
'</td>' +
'<td width="10%" class="dinheiro">' +
'<input type="text" name="vlr_servico[' + id + ']" data-id="' + id + '" onblur="formataDinheiro(this)" id="vlr_servico[' + id + ']" class="dinheiro" />' +
'</td>' +
'<td class="subtotal">' + price + '</td>' +
'<td><a href="javascript:;" onclick="excluirProd(this)">Excluir</a></td>' +
'</tr>';
$('#products_table').append(tr);
$(".dinheiro").maskMoney();
}
updateTotal();
}
function updateTotal() {
}
});