Results 1 to 4 of 4

Thread: not working properly without alert box

  1. #1
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default not working properly without alert box

    Hi,

    I have a prob in my application.. its totally based on calculations.

    Here is sample image for calculations..in the below code i place alert box..if i remove alert box from there i don't get values properly

    Click image for larger version. 

Name:	image.jpg 
Views:	1015 
Size:	8.3 KB 
ID:	5328

    and code is here

    Code:
    function xyz(ival){
        var qty = parseInt($('#qty_'+ival).val());
        var unitPrice = parseInt($('#unitPrice_'+ival).val());
        var id = "select#product_id_"+ival+" option:selected";
        var PriceListId = $('#price_list_id_'+ival).val();
        var Tax = $('#tax_'+ival).val();
        var product_id = $(id).val();
        if(unitPrice!=0){
            $('#total_'+ival).val(qty * unitPrice);
            $.post('/other/api/get-discount/', {product_id: product_id, price_list_id: PriceListId, price: qty}, function(response){
               if(response){
                   $("#discount_"+ival).val(response);
               }
             });
         }
    
                var total = parseInt($('#total_'+ival).val());
                alert();
                if(total!=0){
                    var discount = $('#discount_'+ival).val();
    
                    if(discount!=0){
                        $('#tad_'+ival).val(total - ((total * discount)/100));
                        $('#netTotal_'+ival).val(total - ((total * discount)/100));
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }else{
                        $('#tad_'+ival).val(total - 0);
                        $('#netTotal_'+ival).val(total - 0);
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                 }
    
                 if(Tax!=0){
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                 }
                 
                $( "#tax_"+ival ).keyup(function() {
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                });
        }
    help me out please..
    Last edited by jscheuer1; 01-09-2014 at 02:17 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This:

    Code:
            $.post('/other/api/get-discount/', {product_id: product_id, price_list_id: PriceListId, price: qty}, function(response){
               if(response){
                   $("#discount_"+ival).val(response);
               }
             });
    Is an asynchronous (means that the rest of the script can continue, even if there's no answer yet) request (AJAX). The alert creates a pause whereby the request has time to be processed. The alternative is to perform everything that is dependent upon there being a response within the callback function. In this case I think that would be like so:

    Code:
    function xyz(ival){
        var qty = parseInt($('#qty_'+ival).val());
        var unitPrice = parseInt($('#unitPrice_'+ival).val());
        var id = "select#product_id_"+ival+" option:selected";
        var PriceListId = $('#price_list_id_'+ival).val();
        var Tax = $('#tax_'+ival).val();
        var product_id = $(id).val();
        if(unitPrice!=0){
            $('#total_'+ival).val(qty * unitPrice);
            $.post('/other/api/get-discount/', {product_id: product_id, price_list_id: PriceListId, price: qty}, function(response){
               if(response){
                   $("#discount_"+ival).val(response);
               }
                var total = parseInt($('#total_'+ival).val());
                if(total!=0){
                    var discount = $('#discount_'+ival).val();
    
                    if(discount!=0){
                        $('#tad_'+ival).val(total - ((total * discount)/100));
                        $('#netTotal_'+ival).val(total - ((total * discount)/100));
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }else{
                        $('#tad_'+ival).val(total - 0);
                        $('#netTotal_'+ival).val(total - 0);
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                 }
    
                 if(Tax!=0){
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                 }
                 
                $( "#tax_"+ival ).keyup(function() {
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                });
             });
         }
    
        }
    In an unrelated matter (regardless of the above change), this appears unwise:

    Code:
                $( "#tax_"+ival ).keyup(function() {
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                });
    This should either be assigned separately, outside of the xyz function. Or the keyup event should be removed from this element before attaching, just in case this function has run previously on that element. Namespacing the event would be a good idea at that point as well so as not to conflict with any other potential keyup events. This is done differently (unbind or off) depending upon the version of jQuery being used. Further, the change event is usually (though not always) preferable to the keyup for form values.
    Last edited by jscheuer1; 01-09-2014 at 02:40 PM. Reason: saw unrelated matter
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    udaybabu (01-09-2014)

  4. #3
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi jscheuer1,

    Thank you so much for such a wonderful help.. It works..

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Great! Here's how I would do that other function I mentioned as being a potential problem (prior to version 1.7):

    Code:
                $( "#tax_"+ival ).unbind('keyup.tax').bind('keyup.tax', function() {
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                });
    After:

    Code:
                $( "#tax_"+ival ).off('keyup.tax').on('keyup.tax', function() {
                    var tad = parseInt($('#tad_'+ival).val());
                    var tax = parseInt($('#tax_'+ival).val());
                    if(tax!=0){
                        $('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
                        $('#grandTotal').val($('#netTotal_'+ival).val());
                    }
                });
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. if statement not working properly
    By paramedicbob in forum PHP
    Replies: 4
    Last Post: 12-20-2012, 06:50 AM
  2. Lightbox not working properly
    By IdentityCrisis in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 07-06-2010, 01:48 AM
  3. DHTML not working properly
    By nisarg_sutaria in forum JavaScript
    Replies: 0
    Last Post: 02-24-2010, 05:42 PM
  4. No right click script III (no alert) not working
    By Robert C Tracy in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-24-2005, 09:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •