Add jquery values from input field

Asked

Viewed 34 times

-1

Good afternoon!

I have the following form,

<input type="hidden" id="id_rifa" name="id_rifa" value="qaa">
<input type="hidden" id="id_usuario" name="id_usuario" value="1h0p">
<input type="hidden" id="numeros_bilhetes" name="numeros_bilhetes" value="031,047,048">
<input type="hidden" id="valor_bilhete" name="valor_bilhete" value="154">
<span class="v_total">77</span>
<button type="submit" class="finalizar-btn">Participar</button>
</form>

I have read the following with the data

<li><a class='ativado btn_compra_rifa' data-valor='$valor_rifa' href='JavaScript:void(0);'><i>$i</i></a></li>

My jquery

$('.btn_compra_rifa').click(function() {

 if ($(this).hasClass('desativado')) {
        $(this).removeClass('desativado').addClass('selecionado');

                var valor_rifa = parseFloat($(this).attr("data-valor"));
                var resultado = $(".v_total").html()

                  $('input[name=valor_bilhete]').val(valor_rifa);


      var bilhete_soma = $('#valor_bilhete').val() + valor_rifa;

$('input[name=valor_bilhete]').val(valor_rifa);

alert(bilhete_soma);

It doesn’t add up the values, and when it adds up it doubles at once.. what I’m doing wrong? inserir a descrição da imagem aqui

It had to add 77+77= 154

The logic is that one can choose several numbers, The values add up

1 answer

0


When you take a value of one input, it is stored in a string. I believe Javascript is concatenating the strings instead of add up. If that’s the case, try something like:

var bilhete_soma = parseInt($('#valor_bilhete').val()) + parseInt(valor_rifa);

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.