用于多个文本框的javascript

javascript for multiple textboxes

本文关键字:javascript 文本 用于      更新时间:2023-09-26

我有10个文本框,每个文本框的名称分别为qty1-10、price1-10和total1-10。我所做的只是添加qty1+price1=total1、qty2+price2=total2等值。

这是我当前的代码-

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,依此类推

您可以将类分配给这些文本框,然后执行

var arr = document.getElementByClassName('classname')

您将获得一个文本框数组,可以在该数组上应用循环,然后在循环中执行代码。

(我不是在写代码,因为它是Javascript的基础,你应该自己做)

输入您想要作为参数的分组的ID。然后可以在循环中调用该函数。

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