my problem is that when i dont type anything into the quantity textbox my jquery function calc() does not work. this is because '' = NAN (not a number)
I have tried add this code in my calc() function on cart_actions.js , but with no luck...
[code=jquery]
if(quantity.len gth === 0){
quantity = $(".quantity input", this).val(0);
}
[/code]
all it does is set the value to zero on the textbox, but it does recognize it as a zero in the script, the prices return NAN (AddedQuantity AND totalPrice). I have been scratching my head for weeks please help :) i have done an alert(quantity) aswell and that came back zero, but still AddedQuantity AND totalPrice say NAN.
showcart.php
[code=php]
<input type='text' name='quantity[$id]' size='3' value='$product Qty' /><br /></td>
[/code]
cart_actions.js
[code=jquery]
$(function() {
$("#cart tr .quantity input").change( function() {
var id = $(this).attr("n ame").slice(9, -1);
var quantity = $(this).val();
$.ajax({
type: "GET",
url: "cart_action.ph p",
data: "quantity[" + id + "]=" + quantity,
success: function() {
var startColor = $("#cart tr .quantity input[name*=" + id + "]").parent().par ent().hasClass( "odd") ? "#eeeeee" : "#ffffff";
$("#cart tr .quantity input[name*=" + id + "]").parent().par ent().find("td" ).animate({ backgroundColor : "#ff8" }, 100).animate({ backgroundColor : startColor }, 800);
calcPrice();
},
error: function() {
window.location ("cart_action.p hp?quantity[" + id + "]=" + quantity);
}
});
});
function calcPrice() {
var totalPrice = 0;
var AddedQuantity = 0;
$("#cart tr .quantity").par ent().each(func tion() {
var quantity = $(".quantity input", this).val();
if(quantity.len gth === 0){
quantity = $(".quantity input", this).val(0);
}
var unitPrice = $(".unit_price" , this).text().sl ice(1);
var extendedPrice = quantity*unitPr ice;
var extendedPriceZe ros = extendedPrice.t oFixed(2);
totalPrice += extendedPrice;
AddedQuantity += parseInt(quanti ty);
$(".extended_pr ice", this).html("£" + extendedPrice.t oFixed(2));
$("#total_price ").html("£" + totalPrice.toFi xed(2));
$("#test").html (AddedQuantity) ;
});
if ( totalPrice == 0 ) {
$("#cart").pare nt().replaceWit h("<div id='container'> <h1>Shopping Cart</h1><p>You have no items in your cart. Please <a href='home.php' >Continue to shop</a>!</p></div>");
}
if ( AddedQuantity >= 10 ) {
$("#cart td#shipping").h tml("Free !");
}
if ( AddedQuantity < 10 ) {
$("#cart td#shipping").h tml("£"+AddedQu antity*5);
}
}
});
[/code]
I have tried add this code in my calc() function on cart_actions.js , but with no luck...
[code=jquery]
if(quantity.len gth === 0){
quantity = $(".quantity input", this).val(0);
}
[/code]
all it does is set the value to zero on the textbox, but it does recognize it as a zero in the script, the prices return NAN (AddedQuantity AND totalPrice). I have been scratching my head for weeks please help :) i have done an alert(quantity) aswell and that came back zero, but still AddedQuantity AND totalPrice say NAN.
showcart.php
[code=php]
<input type='text' name='quantity[$id]' size='3' value='$product Qty' /><br /></td>
[/code]
cart_actions.js
[code=jquery]
$(function() {
$("#cart tr .quantity input").change( function() {
var id = $(this).attr("n ame").slice(9, -1);
var quantity = $(this).val();
$.ajax({
type: "GET",
url: "cart_action.ph p",
data: "quantity[" + id + "]=" + quantity,
success: function() {
var startColor = $("#cart tr .quantity input[name*=" + id + "]").parent().par ent().hasClass( "odd") ? "#eeeeee" : "#ffffff";
$("#cart tr .quantity input[name*=" + id + "]").parent().par ent().find("td" ).animate({ backgroundColor : "#ff8" }, 100).animate({ backgroundColor : startColor }, 800);
calcPrice();
},
error: function() {
window.location ("cart_action.p hp?quantity[" + id + "]=" + quantity);
}
});
});
function calcPrice() {
var totalPrice = 0;
var AddedQuantity = 0;
$("#cart tr .quantity").par ent().each(func tion() {
var quantity = $(".quantity input", this).val();
if(quantity.len gth === 0){
quantity = $(".quantity input", this).val(0);
}
var unitPrice = $(".unit_price" , this).text().sl ice(1);
var extendedPrice = quantity*unitPr ice;
var extendedPriceZe ros = extendedPrice.t oFixed(2);
totalPrice += extendedPrice;
AddedQuantity += parseInt(quanti ty);
$(".extended_pr ice", this).html("£" + extendedPrice.t oFixed(2));
$("#total_price ").html("£" + totalPrice.toFi xed(2));
$("#test").html (AddedQuantity) ;
});
if ( totalPrice == 0 ) {
$("#cart").pare nt().replaceWit h("<div id='container'> <h1>Shopping Cart</h1><p>You have no items in your cart. Please <a href='home.php' >Continue to shop</a>!</p></div>");
}
if ( AddedQuantity >= 10 ) {
$("#cart td#shipping").h tml("Free !");
}
if ( AddedQuantity < 10 ) {
$("#cart td#shipping").h tml("£"+AddedQu antity*5);
}
}
});
[/code]
Comment