473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

how can I set a default value to zero if nothing is entered into the text field?

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...
Expand|Select|Wrap|Line Numbers
  1. if(quantity.length === 0){
  2.          quantity = $(".quantity input", this).val(0);   
  3.  
  4.          }
  5.  
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type='text' name='quantity[$id]' size='3' value='$productQty' /><br /></td>
  3.  
  4.  
cart_actions.js

Expand|Select|Wrap|Line Numbers
  1. $(function() {  
  2.  
  3.  $("#cart tr .quantity input").change(function() {  
  4.          var id = $(this).attr("name").slice(9, -1);  
  5.          var quantity = $(this).val();
  6.  
  7.  
  8.          $.ajax({  
  9.              type: "GET",  
  10.              url: "cart_action.php",  
  11.              data: "quantity[" + id + "]=" + quantity,  
  12.              success: function() { 
  13.                  var startColor = $("#cart tr .quantity input[name*=" + id + "]").parent().parent().hasClass("odd") ? "#eeeeee" : "#ffffff";  
  14.                  $("#cart tr .quantity input[name*=" + id + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);  
  15.                  calcPrice();  
  16.              },  
  17.              error: function() {  
  18.                  window.location("cart_action.php?quantity[" + id + "]=" + quantity);  
  19.              }  
  20.          });  
  21.      });  
  22.  
  23.  
  24. function calcPrice() {  
  25.  
  26.     var totalPrice = 0;
  27.     var AddedQuantity = 0;
  28.  
  29.  
  30.      $("#cart tr .quantity").parent().each(function() {  
  31.          var quantity = $(".quantity input", this).val();
  32.          if(quantity.length === 0){
  33.          quantity = $(".quantity input", this).val(0); 
  34.          }
  35.  
  36.          var unitPrice = $(".unit_price", this).text().slice(1);  
  37.  
  38.          var extendedPrice = quantity*unitPrice;
  39.  
  40.          var extendedPriceZeros = extendedPrice.toFixed(2);
  41.  
  42.  
  43.  
  44.          totalPrice += extendedPrice;
  45.          AddedQuantity += parseInt(quantity);  
  46.  
  47.          $(".extended_price", this).html("£" + extendedPrice.toFixed(2));  
  48.          $("#total_price").html("£" + totalPrice.toFixed(2));
  49.          $("#test").html(AddedQuantity);
  50.  
  51.  
  52.      });  
  53.      if ( totalPrice == 0 ) {  
  54.          $("#cart").parent().replaceWith("<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>");  
  55.      } 
  56.      if ( AddedQuantity >= 10 ) {  
  57.          $("#cart td#shipping").html("Free !");  
  58.      }
  59.      if ( AddedQuantity < 10 ) {  
  60.          $("#cart td#shipping").html("£"+AddedQuantity*5);  
  61.      }
  62.  
  63.  
  64.  }  
  65.    });
  66.  
  67.  
Dec 3 '11 #1

✓ answered by omerbutt

hi
why dont you put 0 in the value attribute of the quantity by default ,and then use this method,
Expand|Select|Wrap|Line Numbers
  1. <input type="text" onblur='if(this.value==""){this.value=0;}'>
, i hope i got your problem right.


apart in your existing code , add parseInt() to the line number 38 where you are multiplying the quantity with price.
Expand|Select|Wrap|Line Numbers
  1. var extendedPrice = parseInt(quantity)*parseInt(unitPrice);
regards,
Omer Aslam

4 10064
omerbutt
638 512MB
hi
why dont you put 0 in the value attribute of the quantity by default ,and then use this method,
Expand|Select|Wrap|Line Numbers
  1. <input type="text" onblur='if(this.value==""){this.value=0;}'>
, i hope i got your problem right.


apart in your existing code , add parseInt() to the line number 38 where you are multiplying the quantity with price.
Expand|Select|Wrap|Line Numbers
  1. var extendedPrice = parseInt(quantity)*parseInt(unitPrice);
regards,
Omer Aslam
Dec 3 '11 #2
Dormilich
8,658 Expert Mod 8TB
the problem is line 33. if you re-set quantity, it becomes a JQuery object (while $(…).val() is a number), which on line 38 will produce NaN.

for the calculation itself it would suffice to set quantity to 0.

@Omer: parseInt(JQuery) => NaN
Dec 3 '11 #3
thank you i have added onblur='if(this.value==""){this.value=0;}' and got rid of line

Expand|Select|Wrap|Line Numbers
  1. if(quantity.length === 0){
  2.           quantity = $(".quantity input", this).val(0); 
  3.           }
  4.  
it now seems to work, is there anything else im missing or should that be ok?
Dec 3 '11 #4
omerbutt
638 512MB
hi luke ,
look at Dormilich last post, what he said is to be considered, but if you have removed that line its fine i think, and please select the correct answer which resolved your conflict so that any other person with the same issue who comes googling around could have help too.
regards,
Omer Aslam
Dec 4 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ian D | last post by:
Firstly apologies for the convoluted question. I found this problem whilst building a larger database. I've distilled it down to as small as possible and can send a 200k example to anyone who has...
2
by: Terry Bickle | last post by:
Please forgive me for using the wrong term here or there. I'm an old Excel 4 macro guy who didn't convert to VB and I'm tinkering with an Access 2000 DB. I'm sure there is a simple Access 101...
8
by: Lyn | last post by:
Hi, Can anyone tell me how the initial value displayed in Combo Box is determined when a form is opened? I am loading the dropdown from one field ("CategoryName") of a table, with "ORDER BY ". ...
2
by: Shiye | last post by:
Help...! Hi all, Does any of you know how can determine the default value of a filed to be taken from another feild (already filled) in the same row of the table??? Thanks, Shai
2
by: Eduardo78 | last post by:
I would like to set the default value of a combobox to "nothing" or spaces when my form loads. I am using the dropdown list property becasue i want the end-user to select only values listed in the...
3
by: Eduardo78 | last post by:
I need to have a control where the default value equals nothing, and if an user enters a value It validates that it is a date, if not show an error message or advertise that only dates are allowed....
3
by: harry | last post by:
Using aspnet20 csharp, I have a simple data entry screen that inserts submitted values to a database. I want a default value to be submitted if no value is entered in a textbox. Thanks _harry
9
by: Eric | last post by:
Hi Everyone, I'm writing a UserControl that exposes a property of the type System.Drawing.Image, like this: Public Property DefaultImage() As Image Get Return propDefaultImage End Get...
2
by: Del | last post by:
I have a form call frmMMD-PCKR. This form has seven buttons, each button represents a part. When I click on any of the buttons they open another form called frmTallyTagMMDPCKR. On the...
0
by: phpuser123 | last post by:
I want to display my string str as the defaultinput value and when I run ,oly the first part of the word is displayed. It skips everything after the blank space.How do I sort this out? <% ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.