Why won't this var change value?
So I have this jquery code and it works except for one specific part. I am doing a quantity check for a shopping cart system and I need to prevent the default form submission of the submit button and check all the quantities with a $.post() and checking the database. I have confirmed that all aspects work for the checking and outputting to the screen when and where they need to. If I alert(bad) inside the else{} it shows me the value I expect but it does not seem to hold that value or something after the .each() runs which is where I check it's value and submit the form if it's still false in value. Any insight would be great.
- $('#paypal-submit-button').click(function(e){
- var bad = false;
- e.preventDefault();
- $('.qty-input').each(function(){
- var qty = parseInt($(this).val());
- var id = parseInt($(this).attr('prod_id'));
- var qty_id = String($(this).attr('qty-input-num'));
-
- $.post('check_avail_qty.php', {pid:id, quantity:qty}, function(data){
- if(data == "good")
- { }
- else
- {
- $('#prod-qty-check'+qty_id).html('Oh No! It seems we have run out of this item. Please remove it from your cart to continue.');
- var bad = true;
- alert(bad);
- }
- });
- })
- if(bad === false){ $('#cart-submit-form').submit(); }
- });