вы можете попробовать это?
$('#submit').click(function() {
var mpg= $('#mpg').val();
if(mpg=='') {
alert("The average cannot be empty");
$('#mpg').focus();
return false; // cancel submission
}
if (isNaN(mpg)) {
alert("The value you entered is not a valid number");
$('#mpg').focus();
return false; // cancel submission
}
mpg = parseFloat(mpg);
if (mpg==0) {
alert("MPG cannot be 0"); // or you will divide by 0 later
$('#mpg').focus();
return false; // cancel submission
}
var distance = $('#distance').val();
distance = isNaN(distance)? 0: parseFloat(distance);
var costlitre = $('#costlitre').val()
costlitre = isNaN(costlitre)?0: parseFloat(costlitre);
// Work out litres needed
if(symbol=='$') {
gallons = distance / mpg;
cost = gallons * costlitre
cost = cost.toFixed(2);
} else {
gallons = distance / mpg;
litresneeded = gallons * litres;
cost = litresneeded * costlitre;
cost = cost.toFixed(2);
}
$('#total').html(symbol+cost);
$('#four').fadeOut("slow", function() {
Cufon.replace('h1, p, .tip, #units, #price, #total', { color: '-linear-gradient(#fff, #fff)'});
$('#five').fadeIn("slow");
});
return false; // cancel submission - change to true to submit
}