Help me with jQuery [closed]

-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>

jQuery code:

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", "#menos") work, I'm having problems.

    
asked by anonymous 08.12.2016 / 02:09

1 answer

0

Your code had some errors there in the click function

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

You have closed two parentheses instead of one, because it is already closed at the end. And what was missing there, was how you have more than one label, you need to identify which label you want to get the value attribute, in case you put:

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

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

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

And there were also some "; The corrected code looks like this, I hope I 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>
    
08.12.2016 / 02:35