2010-02-25 5 views
2

동적으로 생성 된 텍스트 상자에 추가됩니다하지 않는 것 같습니다 ...하지만 제대로 작동 나던 ....자바 스크립트 이벤트는 내가 자바 스크립트에서 동적으로 추가 텍스트 상자에 대한 자바 스크립트를 onKeyUp에 추가

var cell4 = row.insertCell(3); 
cell4.setAttribute('align','center') 
var e3 = document.createElement('input'); 
e3.type = 'text'; 
e3.name = 'txtqt' + iteration; 
e3.id = 'txtqt' + iteration; 
e3.onkeyup = totalAmount(event,this,'tblsample');//Adding this lines doesnt work 
e3.size = 10; 
cell4.appendChild(e3); 

그러나 때 나는 일

e3.onkeyup = totalAmount; 

... 사용 여기 내 자바 스크립트 함수,

기능 totalAmount이다 (예, OBJ, tblid) {

,
var tbl = document.getElementById(tblid); 
//alert(tbl); 
var tblRows = tbl.rows.length; 
//alert(tblRows); 
var result =0; 

var str1; 
if (obj != null) { 
    str1 = obj.id; 
} else { 
    str1 = this.id; 
} 
var lastChar = str1.substring(5,str1.length); 
//alert(lastChar); 

if(str1=='txtqt'+lastChar) 
{ 
    var str2 = 'txtup'+lastChar; 
    var str3 = 'txtAmount'+lastChar; 
    var txtDeduct = document.getElementById(str1).value; 
    var txtAmt = document.getElementById(str2).value; 
    var txtTotal = document.getElementById(str3); 
    var totRes = txtAmt*txtDeduct; 
    //var res = formatNumber(totRes,2) 
    txtTotal.value = totRes.toFixed(2) 
    document.getElementById('txttotAmount').value = totRes.toFixed(2); 


    for(i=1;i<=tblRows;i++) 
    { 
    //alert(tblRows); 
     txtTotID = 'txtAmount'+i; 
     if(document.getElementById(txtTotID).value!='') 
     { 

      result =parseFloat(result) + parseFloat(document.getElementById(txtTotID).value); 

      //var res= formatNumber(result,2) 
      document.getElementById('txtTotalAmount').value = result.toFixed(2); 
      document.getElementById('txttotAmount').value = result.toFixed(2); 
      //document.getElementById('txtTotalAmount').value = result; 
     } 

    } 

} 

}

답변

1

: 크로스 브라우저 호환성 수 있도록,

e3.onkeyup = function(event){ totalAmount(event,this,'tblsample'); } 

그러나 그것을 할 수있는 더 나은 방법 것

,536 : 해당 기능을 사용하여 이벤트를 추가 한 후

function addEvent(obj,type,fn){ 
    if (obj.addEventListener){ 
     obj.addEventListener(type,fn,false); 
    } else if(obj.attachEvent){ 
     obj["e"+type+fn]=fn; 
     obj[type+fn]=function(){ 
      obj["e"+type+fn](window.event); 
     }; 
     obj.attachEvent("on"+type,obj[type+fn]); 
    }; 
}; 

그리고 : addEvent 기능을 사용하는

addEvent(e3,'keyup',function(event){ totalAmount(event,this,'tblsample'); }); 

이벤트를 처리하는 데 훨씬 좋은 방법입니다. 이 방법으로 전환하는 것이 좋습니다.

+0

@sparkyfied '이벤트가 정의되지 않았습니다.'오류 ... –

+0

@chandru_cp 죄송합니다. try addEvent (e3, 'keyup', function (event) {totalAmount (event, this, 'tblsample');}); – jamesmhaley

+0

@chandru_cp 해당 수정 사항을 반영하도록 위 게시물을 업데이트했습니다. – jamesmhaley

1

onkeyup 함수이다. 반환 값을 totalAmount(event,this,'tblsample');으로 전달하면 함수를 반환하지 않는 한 작동하지 않습니다.

e3.onkeyup = totalAmount;이면 충분합니다. totalAmount 내부에 다음

.. 그래서

function totalAmount(event) { 
    alert(this); // this is the e3 object 
} 

당신이 this하고 'tblsample'인수를 필요로하는 경우, 당신은 내부의 this 키워드를 통해 액세스 할 수 있도록 당신이 E3 객체에 추가 제안 totalAmount 기능 :

e3.otherScope = this; 
e3.tblid = 'tblsample; 
e3.onkeyup = totalAmount; 

및 ..

function totalAmount(event) { 
    alert(this); // this is the e3 object 
    alert(this.otherScope); // the `this` object in the other scope 
    alert(this.tblid); // 'tblsample' 
} 
,

또는 당신은 단순히 할 수

당신은 익명 함수에서 함수 호출을 래핑 할 필요가
var otherScope = this; 
e3.onkeyup = function(event) { 
    totalAmount(event, otherSope, 'tblsample'); 
}; 
관련 문제