|
I'm doing a school project and I'm trying to add up several text boxes, however some of them are blank and when I calculate them I get a NaN result. Here is my code:
qty1= parseFloat(document.form1.qty_prt.value)
qty2= parseFloat(document.form1.qty_mon.value)
qty3= parseFloat(document.form1.qty_comp.value)
sub_tot=parseFloat(document.form1.subt.value)
tax_tot=parseFloat(document.form1.tax.value)
grand_tot=parseFloat(document.form1.grandt.value)
qty_tot=eval(qty1+qty2+qty3)
alert("quantity is " + qty_tot)
|
|
|
Draw the text boxes inside a for loop or wat ever method is convinient for u such that all textboxes id should start with the same text and end with the number as it is incremented in the loop.
for(int i=0;i<10;i++){
out.println("<input type='text' id='text+i' >");
}
Then in java script calculate the values as:
function addAllTextBoxValues(){
var total;
for(i=0;i<10;i++){
var txtid = text+i;
total += document.getElementById(txtid).value;
}
}
This will add the values in all the text boxes even if not entered and gives u the total.
|
|
|
|
|
|
|
|
|
// |