mardi 5 septembre 2017

JQuery, values wont update after clicking checkbox

Got my little calculator working in jquery, but i got a small problem. When i check the checkbox it multiples the input of field 1 and 2 with 10. But if i input the values in field 1 and 2 BEFORE checking the checkbox it wont update with the new value.

Can someone give me a hint on how to do this?

Thank you.

http://ift.tt/2vHiX3l

Code:

  <!DOCTYPE html>
    <html>
    <head>
        <script src="jquery.js"></script>
        <script>
            $(document).ready(function(){
                $('#a1').keyup(calculate);
                $('#a2').keyup(calculate);
                $('#a3').keyup(calculate);
            });
            function calculate(e) {

            if ($('input.checkbox_check').is(':checked')) {
                $('#a4').val(($('#a1').val() * $('#a2').val())*0.6);
                $('#a3').val($('#a1').val() * $('#a2').val());          
            } else {
                $('#a3').val($('#a1').val() * $('#a2').val());
                $('#a4').val($('#a3').val() * 0.5);
            }}
        </script>
    </head>
    <body>


    <div>Lengde <input id="a1" type="text" value="0" /></div>
    <div>Bredde <input id="a2" type="text" value="0" /></div>
    <div><input type="checkbox" name="checkbox" class="checkbox_check"></div>
    <div>Antall kvm <input id="a3" type="text" value="0" /></div>
    <div>Pris <input id="a4" type="text" value="0" /></div>
    </body>
    </html>




Aucun commentaire:

Enregistrer un commentaire