如何在javascript中动态创建的行中获得2列的差异

时间:2015-02-03 04:56:47

标签: javascript jquery difference array-difference

我有一个动态创建的表有3列。我需要在Col 3中填充Col 2和Col 1的区别。我不知道如何继续动态创建的表。我在保存函数中读取表值。您能否建议如何包含差异代码?

JS:

function save()
{
 var eval_tbl = document.getElementById('bagger_reading_list');
 var eval_row_array = [];
        for (i=0; i<eval_tbl.rows.length; i++)
        {
            var tableRow = {
                Start_Reading : eval_tbl.rows[i].cells[1].firstElementChild.value,
                End_Reading : eval_tbl.rows[i].cells[2].firstElementChild.value,
                Total_Reading : eval_tbl.rows[i].cells[3].firstElementChild.value, \\ I need the difference of End-Reading - Start_Reading to be displayed on Total_Reading of eah row created.
            }
            eval_row_array[eval_row_array.length] = tableRow;
        }

}

3 个答案:

答案 0 :(得分:2)

使用parseFloat()将值转换为Float(只是为了安全)并将其添加。这样做:

Total_Reading : parseFloat(eval_tbl.rows[i].cells[2].firstElementChild.value) - parseFloat(eval_tbl.rows[i].cells[1].firstElementChild.value),

因此,您的保存功能应如下所示:

function save()
{
 var eval_tbl = document.getElementById('bagger_reading_list');
 var eval_row_array = [];
        for (i=0; i<eval_tbl.rows.length; i++)
        {
            var tableRow = {
                Start_Reading : eval_tbl.rows[i].cells[1].firstElementChild.value,
                End_Reading : eval_tbl.rows[i].cells[2].firstElementChild.value,
                Total_Reading : parseFloat(eval_tbl.rows[i].cells[2].firstElementChild.value) - parseFloat(eval_tbl.rows[i].cells[1].firstElementChild.value)
            }
            eval_row_array[eval_row_array.length] = tableRow;
        }

}

读取:parseFloat() | MDN

答案 1 :(得分:1)

试试这个:

 function save()
    {
     var eval_tbl = document.getElementById('bagger_reading_list');
     var eval_row_array = [];
            for (i=0; i<eval_tbl.rows.length; i++)
            {
var startReading = eval_tbl.rows[i].cells[1].firstElementChild.value;
var endReading = eval_tbl.rows[i].cells[2].firstElementChild.value;
var totalReading = parseFloat(endReading) - parseFloat(startReading);
                var tableRow = {
                    Start_Reading : startReading,
                    End_Reading : endReading,
                    Total_Reading : totalReading
                }
                eval_row_array[eval_row_array.length] = tableRow;
            }

    }

答案 2 :(得分:0)

我写了一个小代码。检查它是否符合您的需要

 function saved()
{

         var eval_tbl = document.getElementById('Table');
         var eval_row_array = [];
         for (i=0; i<eval_tbl.rows.length; i++)
        {

            var tableRow = {
                Start_Reading : eval_tbl.rows[i].cells[0].innerHTML,
                End_Reading : eval_tbl.rows[i].cells[1].innerHTML,
                Total_Reading :parseInt(eval_tbl.rows[i].cells[0].innerHTML) - parseInt(eval_tbl.rows[i].cells[1].innerHTML)
            }
             eval_tbl.rows[i].cells[2].innerHTML=parseInt(eval_tbl.rows[i].cells[0].innerHTML) - parseInt(eval_tbl.rows[i].cells[1].innerHTML)
            eval_row_array[eval_row_array.length] = tableRow;

        }

}