PDA

View Full Version : not working properly without alert box



udaybabu
01-09-2014, 09:34 AM
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

5328

and code is here


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

jscheuer1
01-09-2014, 02:30 PM
This:


$.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:


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:


$( "#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.

udaybabu
01-09-2014, 02:54 PM
Hi jscheuer1,

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

jscheuer1
01-11-2014, 04:31 AM
Great! Here's how I would do that other function I mentioned as being a potential problem (prior to version 1.7):


$( "#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:


$( "#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());
}
});