Help me with the jQuery

Asked

Viewed 36 times

-1

PHP code with HTML:

<h2 ><?php the_title() ; ?></h2>

<ul class="galeria" >
<?php the_title() ?>
    <li class="pizzadisponivel"><label value="29,99"><?php the_post_thumbnail('medium');?></label></li>
    <spam> <?php the_content() ?> </spam>
    <button  id="mais"  >+1</button>
    <button  id="menos"  >-1</button>
</ul>

<?php
endwhile; 
endif;
?>
    <label id="teste"=>R$0.00</label>   
    <button  class="col-12" id="btn"  >Finalizar Compra</button>

Code jQuery:

var Total = parseFloat('0');

var P = $('label').attr('value');

var Pp = (P.replace(/,/,'.'));

$('#mais').click(function(e)){
    Total += parseFloat(Pp)
    $("#teste").html('R$' + parseFloat(Total).toFixed(2));
});

$('#menos').click(function(e)){
    Total -= parseFloat(Pp)
    $("#teste").html('R$' + parseFloat(Total).toFixed(2));
});

$('#btn').click(function(){
alert('Sua compra foi finalizada.');

I have this code, I would like to make the buttons ("#more", "#less") work, I’m having problems.

1 answer

0


Your code had some errors there in the click function

$('#mais').click(function(e)){

You closed two parentheses instead of one, because this already closed there at the end. And what was missing there, was how Voce has more than one label, Voce needs to identify which label you want to take the value attribute, in case you put :

    var P = $('label').attr('value');

When instead Voce should identify which label you want to take value, in this case it is the label that is inside the <li> with the pizzadisponivel class, so I did so:

 var P = $('.pizzadisponivel label').attr('value');

And also missing some ";". The corrected code was like this, I hope to have helped:

    <script>
        var Total = parseFloat('0');

        var P = $('.pizzadisponivel label').attr('value');

        var Pp = (P.replace(/,/,'.'));



$('#mais').click(function(e){
    Total += parseFloat(Pp);
    $("#teste").html('R$' + parseFloat(Total).toFixed(2));
});


$('#menos').click(function(e){
    Total -= parseFloat(Pp);
    $("#teste").html('R$' + parseFloat(Total).toFixed(2));
});

</script>

Browser other questions tagged

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