2013-03-17 3 views
0

자바 스크립트 함수에 변수를 전달해야합니다.이 함수는 계산을 수행하고 폼의 다른 편집 상자에 답을 반환합니다. 10 줄의 편집 상자가 있고 10 개의 별도의 자바 스크립트 기능을 갖고 싶지 않으므로 패스해야합니다.변수에 자바 스크립트 전달 elementid

onchange="calc_totalcost('L1')" 

그래서, 편집 상자 1 L1_edit1에 대한 그때 (입력) 이름 에디트 박스입니다 'L1_qty'로 변환 할 기능에 L1을 보낼 필요가 다음 HTML에

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

그 내용을 사용하여 계산을 수행합니다. 희망이 맞는가? 감사합니다.

+0

거기에 질문이 있습니까? – Aiias

+0

for 루프를 사용하십시오! – spaceman12

답변

0

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; 
} 
관련 문제