Javascript在变量中传递elementid

Javascript在变量中传递elementid,javascript,Javascript,我需要将一个变量传递给javascript函数,该函数将执行计算并将答案返回到表单上的另一个编辑框。我需要通过,因为我有10行编辑框,不想有10个单独的javascript函数 function calc_totalcost(line) { $line_qty=line+"_qty"; $line_totcost=line+"_totcost"; $line_unitcost=line+"_unitcost"; $totcost=$line_qty

我需要将一个变量传递给javascript函数,该函数将执行计算并将答案返回到表单上的另一个编辑框。我需要通过,因为我有10行编辑框,不想有10个单独的javascript函数

function calc_totalcost(line)
{   
        $line_qty=line+"_qty";
    $line_totcost=line+"_totcost";
    $line_unitcost=line+"_unitcost";

    $totcost=$line_qty.value*$line_unitcost.value;
    document.getElementById('$line_totcost').value = $totcost;
}
在html上:

onchange="calc_totalcost('L1')"
因此,在L1_edit1的editbox 1上,我需要将L1发送到函数,然后函数将转换为“L1_qty”,这是一个editbox(输入)名称,它将使用其内容执行计算。希望这有意义?
谢谢

您有一些问题,包括函数中不需要document.getElementById的最后一行,而其他所有函数都需要它

function calc_totalcost(line) {   
    var $line_qty     = document.getElementById(line+"_qty");
    var $line_totcost = document.getElementById(line+"_totcost");
    var $line_unitcost= document.getElementById(line+"_unitcost");

    var $totcost=$line_qty.value*$line_unitcost.value;
    $line_totcost.value = $totcost;
}

有问题吗?