0

遇到功能上的麻烦。实际上我需要使这个函数对一些文本字段执行计算。当我在一条线上工作时没有问题。但是最近有人要求做一个多行的表(可以动态添加一行)所以,我做了以下功能,它不仅可以复制行,而且可以更改所有相关字段的id,所以我将类添加到这些字段. 因此我进行如下处理:

function clone(line) {

     newLine = line.cloneNode(true);
     line.parentNode.appendChild(newLine);
     var tab = document.getElementsByClassName('libelle_debours')
     var i = -1;
     while (tab[++i]) {
         tab[i].setAttribute("id", "_" + i);
     };
     var cab = document.getElementsByClassName('ht_no_tva')
     var j = -1;
     while (cab[++j]) {
         cab[j].setAttribute("id", "_" + j);
     };
     var dab = document.getElementsByClassName('ht_tva')
     var k = -1;
     while (dab[++k]) {
         dab[k].setAttribute("id", "_" + k);
     };
     var eab = document.getElementsByClassName('taux')
     var l = -1;
     while (eab[++l]) {
         eab[l].setAttribute("id", "_" + l);
     };
     var fab = document.getElementsByClassName('tva')
     var m = -1;
     while (fab[++m]) {
         fab[m].setAttribute("id", "_" + m);
     };

 }

 function delRow() {
     var current = window.event.srcElement;
     //here we will delete the line
     while ((current = current.parentElement) && current.tagName != "TR");
     current.parentElement.removeChild(current);
 }

问题实际上是用于进行计算的第二个函数:

 function calcdebours() {
            var taux = document.getElementById('debours_taux_tva').value;
            var ht_no_tva = document.getElementById('debours_montant_ht_no_tva').value;
            var ht_tva = document.getElementById('debours_montant_ht_tva').value;
            var tva = Math.round((((ht_tva) * (taux)) / 100) * 100) / 100;;
            if (taux == '') {
                taux = 0;
            }
            if (ht_no_tva == '') {
                ht_no_tva = 0;
            }
            if (ht_tva == '') {
                ht_tva = 0;
            }
            document.getElementById('debours_montant_tva').value = tva;
            document.getElementById('debours_montant_ttc').value = (tva) + parseFloat(ht_tva) + parseFloat(ht_no_tva)


        }

        function 

montant_debours() {
        var ttc = document.getElementById('debours_montant_ttc').value;
        var ttc2 = document.getElementById('debours_montant_ttc2').value;
        if (ttc == '') {
            var ttc = 0;
        } else {
            var ttc = document.getElementById('debours_montant_ttc').value;
        }
        if (ttc2 == '') {
            var ttc2 = 0;
        } else {
            var ttc2 = document.getElementById('debours_montant_ttc2').value;
        }
        tx = parseFloat(ttc) + parseFloat(ttc2);
        document.getElementById('ttc_cheque').value = Math.round(tx * 100) / 100;
    }

由于 Id 不一样,我是否必须创建与行一样多的函数?

是否可以使用单个函数来处理每一行?

如果是这样,你能告诉我怎么做吗?

4

1 回答 1

0

如果我没记错的话,您可以使用 for 循环并将增量附加到元素 ID 的末尾。像这样:

trs = document.getElementById('container Id').getElementsByTagName('tr');
For (var i = 1, i <= trs.length; i++)
{
    var el = document.getElementById('debours_montant_ttc' + i);
}
于 2015-08-27T04:48:59.913 回答