Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement dependent input fields Jquery?

I'm building a invoice system where item name and price retrieved from database . Price is dependent on item name .

Now i have to do this .

Price: <Input type="text" id="price" name="price" />
Qty : <Input type="text" id="qty" name="qty" />
Tax : <Input type="text" id="tax" name="tax" />
Discount : <Input type="text" id="disc" name="disc" />
Total : <Input type="text" id="total" name="total"  value=" echo $total;"/>

I want to autofill total input field without reloading with this following formula

<?Php 
$total = $price * $qty + $tax - $disc;
?>
like image 222
Utam Sharma Avatar asked Apr 17 '26 07:04

Utam Sharma


2 Answers

need to assign class in your html, and some jquery script,

//Assign Amount class in your 4 html fields

    Price: <Input type="text" id="price" name="price" class="amount" />
    Qty : <Input type="text" id="qty" name="qty" class="amount"/>
    Tax : <Input type="text" id="tax" name="tax" class="amount"/>
    Discount : <Input type="text" id="disc" name="disc" class="amount"/>
    Total : <Input type="text" id="total" name="total"  value=" echo $total;"/>

    $( document ).ready(function() {
        $('input.amount').keyup(function(){   
        var price = $("#price").val();
        var qty = $("#qty").val();
        var tax = $("#tax").val();
        var disc = $("#disc").val();
        var total = $("#total").val();
        total = ((parseFloat(price) * parseFloat(qty) + parseFloat(tax) - parseFloat(disc);
        $("#total").val(parseFloat(total));
       });
    });
like image 61
Muhammad Akber Khan Avatar answered Apr 19 '26 21:04

Muhammad Akber Khan


It should be as,

  $( document ).ready(function() {
    $('input.amount').keyup(function(){   
    var price = $("#price").val();
    var qty = $("#qty").val();
    var tax = $("#tax").val();
    var disc = $("#disc").val();
    var total = $("#total").val();
      var total = (parseFloat(price) * parseFloat(qty)) + (parseFloat(tax) - parseFloat(disc));
    $("#total").val(parseFloat(total));
   });
});
like image 40
CCoder Avatar answered Apr 19 '26 21:04

CCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!