2012-02-15 52 views
0

我有10個文本框名稱qty1-10,price1-10和total1-10爲每個這些。我做的是增加qty1 +價格1 =共1頁,qty2 + price2 =共2等的值..JavaScript的多個文本框

這是我當前的代碼 -

function Add() 
    { 
    var qty1 = document.getElementById('qty1').value; 
    var u_price1 = document.getElementById('price1').value; 
    if (qty1 == "") { 
     if (u_price1 == "") { 
      document.form1.total1.value = "null"; 
      return; 
      } 
     document.form1.total1.value = "null"; 
     return; 
     } 
    else 
    { 
     document.form1.total1.value = Number(qty1) + Number(u_price1); 
    }  
    } 

現在我想寫一個for循環,將使用相同的代碼,但將qty1的名稱更改爲qty2等等。

+0

謝謝,現在高達55%。將做更多。 – user970225 2012-02-15 18:56:07

回答

1

您可以指定類的文本框,然後做

var arr = document.getElementByClassName('classname')

您將獲得文本框的陣列上,您可以申請循環,然後在循環中執行代碼。

(我沒有寫代碼,因爲它的Javascript基礎知識,你應該自己做)

0

傳入您想要作爲參數的分組的ID。然後你可以在循環中調用該函數。

function Add(ID) { 
    var qty1 = document.getElementById('qty'+ID).value; 
    var u_price1 = document.getElementById('price'+ID).value; 
    ... 
} 
+0

但我只有1個提交按鈕。那麼你能告訴我如何在文本框級別做到這一點嗎? – user970225 2012-02-15 18:58:20

+0

添加更多不提交調用JS功能按鈕的按鈕。 – 2012-02-15 19:02:59