Javascript事件不存在';似乎没有添加到动态生成的文本框中

Javascript事件不存在';似乎没有添加到动态生成的文本框中,javascript,textbox,onkeyup,Javascript,Textbox,Onkeyup,我为javascript中动态添加的文本框添加了onkeyup javascript。。。但它似乎不起作用 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.onkey

我为javascript中动态添加的文本框添加了onkeyup javascript。。。但它似乎不起作用

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;
它起作用了。。。这是我的javascript函数

功能总金额(e、obj、tblid) {

var tbl=document.getElementById(tblid);
//警报(tbl);
var tblRows=tbl.rows.length;
//警报(tblRows);
var结果=0;
var-str1;
如果(obj!=null){
str1=对象id;
}否则{
str1=this.id;
}
var lastChar=str1.substring(5,str1.length);
//警报(lastChar);
if(str1=='txtqt'+lastChar)
{
var str2='txtup'+lastChar;
var str3='txtAmount'+lastChar;
var txtdetraction=document.getElementById(str1).value;
var txtmat=document.getElementById(str2).value;
var txtotal=document.getElementById(str3);
var totRes=txtAmt*txtDecrete;
//var res=格式编号(总计,2)
TXTOTAL.value=TOFRES.toFixed(2)
document.getElementById('txttoAmount')。value=totRes.toFixed(2);

for(i=1;i
onkeyup
是一个函数。如果将
totalAmount(事件,此为“tblsample”)的返回值传递给它;
它将不起作用(除非它返回函数)

e3.onkeyup=totalAmount;
可能就足够了

然后在总数内

function totalAmount(event) {
    alert(this); // this is the e3 object
}
因此,如果您需要
this
和'tblsample'参数,我建议您将它们添加到e3对象中,以便您可以通过totalAmount函数中的
this
关键字访问它们:

e3.otherScope = this;
e3.tblid = 'tblsample;
e3.onkeyup = totalAmount;
e3.onkeyup = function(event){ totalAmount(event,this,'tblsample'); }
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(e3,'keyup',function(event){ totalAmount(event,this,'tblsample'); });

或者你可以简单地做

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

您需要将函数调用包装在匿名函数中:

e3.otherScope = this;
e3.tblid = 'tblsample;
e3.onkeyup = totalAmount;
e3.onkeyup = function(event){ totalAmount(event,this,'tblsample'); }
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(e3,'keyup',function(event){ totalAmount(event,this,'tblsample'); });
但为了实现跨浏览器兼容性,更好的方法是使用addEvent函数:

e3.otherScope = this;
e3.tblid = 'tblsample;
e3.onkeyup = totalAmount;
e3.onkeyup = function(event){ totalAmount(event,this,'tblsample'); }
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(e3,'keyup',function(event){ totalAmount(event,this,'tblsample'); });
然后使用该函数添加事件:

e3.otherScope = this;
e3.tblid = 'tblsample;
e3.onkeyup = totalAmount;
e3.onkeyup = function(event){ totalAmount(event,this,'tblsample'); }
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(e3,'keyup',function(event){ totalAmount(event,this,'tblsample'); });

这是一种更好的处理事件的方法。我建议您切换到这种方法。

@chandru\cp抱歉。请尝试addEvent(e3,'keyup',function(event){totalAmount(event,this,'tblsample');})@chandru_cp太好了。我建议你考虑使用不同的库。Prototype和jQuery以及两个很棒的工具。它们都有很好的事件处理功能和一系列其他功能,这使得编码更快、更简单,最重要的是跨浏览器。@Sparkifyed我和chandru发布了这个问题……我是u唱jquery,但他现在正在学习。。。