为动态填充的文本框调用javascript函数

时间:2011-09-09 05:58:41

标签: javascript

您好我使用javascript动态生成了文本框。我写了一个函数来找时差。 如果我填充4行文本框(多行),则该函数仅适用于第一行。 我无法访问剩余行的函数。

canu给我任何建议吗?

提前致谢。

动态填充文本框的代码

function addChl_Element()
{
    var num_box = document.getElementById("num_2g_halt_text").value;    
    if(!isNaN(num_box))
    { 

        for(var i=0; i<num_box; i++)    
        {      
            var oldTable=document.getElementById('halttable');
            var bBox = oldTable.getElementsByTagName('tbody')[0];
            var newRow=document.createElement('tr');
            var newTimeDown = document.createElement('td');
            var newDateDown = document.createElement('td');
            var newTimeBack = document.createElement('td');
            var newDateback = document.createElement('td');
            var newDurationOutage = document.createElement('td');
            newSiteName.innerHTML = "<input type='text' name='site_name3[]'      id='site_name3'>";
            newTimeDown.innerHTML = "<input type='text' name='time_down3[]' id='time_down3'>";
            newDateDown.innerHTML = "<input type='text' name='date_down3[]' id='date_down3'>";
            newTimeBack.innerHTML = "<input type='text' name='time_back3[]' id='time_back3'>"; 
            newDateback.innerHTML = "<input type='text' name='date_back3[]' id='date_back3'>"; 
            newDurationOutage.innerHTML = "<input type='text' name='duration_of_outage3[]' id='duration_of_outage3' onfocus = 'dura2(this.value);'>"; 


            newRow.appendChild(newTimeDown);
            newRow.appendChild(newDateDown);
            newRow.appendChild(newTimeBack);
            newRow.appendChild(newDateback);
            newRow.appendChild(newDurationOutage);
            bBox.appendChild(newRow);
        }
    }
}

查找时差的函数代码:

function dura2()
{
    var date_bk = document.getElementById("date_back3").value;
    var time_back = document.getElementById("time_back3").value;
    var date_down = document.getElementById("date_down3").value;
    var time_down = document.getElementById("time_down3").value;
    var testArr = new Array();
    testArr[0] = date_bk;
    testArr[1] = time_back;
    var down=new Array();
    down[0] = date_down;
    down[1] = time_down;
    var back_dur = new Date(testArr.join(" "));
    var down_dur = new Date(down.join(" "));
    var duration = Math.abs((back_dur) - (down_dur));
    var cr_dr = Math.round(duration/60/1000);
    document.getElementById("duration_of_outage3").value = cr_dr;
}

0 个答案:

没有答案