简体   繁体   English

用于计算的javascript函数

[英]javascript function for calculating

I meet a trouble with a function. 我在功能上遇到麻烦。 actually I need to make this function to perform a calculation on some text fields. 实际上,我需要使此函数在某些文本字段上执行计算。 When I worked on a single line no problems. 当我在一行上工作时,没有问题。 But recently, someone asked to make a table with multiple lines (one line can be added dynamically) so, I do the following function so that it can not only duplicate line but id change all the fields concerned, so I add class to these fields. 但是最近,有人要求创建一个包含多行的表(可以动态添加一行),因此,我执行以下功能,以便它不仅可以重复行,而且可以更改所有相关字段,因此我将类添加到这些字段中。 therefore I proceed as follows: 因此,我进行如下操作:

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);
 }

The problem in fact is the second function that is used to make the calculation: 实际上,问题是用于进行计算的第二个函数:

 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;
    }

As Id are not the same, do I have to create as many functions there are lines? 由于Id并不相同,我是否必须创建尽可能多的行数功能?

Is it possible to fit a single function to process each line? 是否可以使用单个函数来处理每一行?

If so can you tell me how? 如果可以,您能告诉我如何?

If I'm not mistaken you can use for loop and append increment to the end of element's id. 如果我没记错的话,可以使用for循环并将增量添加到元素ID的末尾。 Like this: 像这样:

trs = document.getElementById('container Id').getElementsByTagName('tr');
For (var i = 1, i <= trs.length; i++)
{
    var el = document.getElementById('debours_montant_ttc' + i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM