I can not resolve to use SelectPicker because in the added field it keeps the previous information and does not sum ... The normal select already works correctly. I need to put this function but I do not know where.
$('.selectpicker').selectpicker('refresh');
$('.multi-field-wrapper').each(function() {
//var max_fields = 10; //maximum input boxes allowed
var wrapper = $('.multi-fields', this); //Fields wrapper
var add_button = $(".add-field"); //Add button ID
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
$('.multi-field:first-child', wrapper).clone(true).appendTo(wrapper).find('input').val().focus();
//re-init mask money to apply new added input
initMaskMoney();
});
$('.multi-field .remove-field', wrapper).click(function() {
if ($('.multi-field', wrapper).length > 1){
$(this).parent('.multi-field').remove();
somarValores();
}
});
});
initMaskMoney();
function initMaskMoney() {
$('input[id^="demo"]').maskMoney({thousands:'', decimal:','});
}
$('.custo,.quantidade').on('change', function() {
somarValores();
});
function somarValores() {
var amount_sum = 0;
//calculate total worth of money
$('.line').each(function(){;
// var custo = $(this).find(".custo").val();
var custo = $(this).find(':selected').attr('data-price');
var custoConvertido = parseFloat(custo.replace(",", "."));
var quantidade = $(this).find(".quantidade").val();
var total = custoConvertido * quantidade
if (total > 0){
amount_sum += parseFloat(total);
sum = amount_sum.toFixed(2).toString().replace(/\./g, ',');
console.log(amount_sum);
console.log(sum);
}
});
if(amount_sum == 0)
sum = "0,00";
else
sum = amount_sum.toFixed(2).toString().replace(/\./g, ',');
$('#valor').html(sum);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field line">
<select id="demo[1]" class="selectpicker custo" name="custo[]">
<option value="10" data-price="150">Teste</option>
<option value="20" data-price="450">Teste</option>
<option value="10" data-price="350">Teste</option>
</select>
<input id="quantidade" class="quantidade" type="number" name="quantidade[]" value="1">
<button type="button" class="remove-field btn btn-danger">
Excluir
</button>
<br>
</div>
</div>
</div>
<button type="button" class="add-field btn btn-primary m-l-20">
Adicionar Campos
</button>
<div class="title">Total do Atendimento</div>
<span class="amount" count-to-currency="1123.99" value="0" duration="2">
R$ <span id="valor">0,00</span>
</span>