Javascript事件似乎没有添加到动态生成的文本框中

时间:2010-02-25 12:23:23

标签: 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.onkeyup = totalAmount(event,this,'tblsample');//Adding this lines doesnt work
e3.size = 10;
cell4.appendChild(e3); 

但是当我使用

e3.onkeyup = totalAmount;

它有用......这是我的javascript函数,

function totalAmount(e,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;
        }

    }

}

}

2 个答案:

答案 0 :(得分:1)

onkeyup是一个功能。如果你传递totalAmount(event,this,'tblsample');的返回值它将不起作用(除非它返回一个函数)。

e3.onkeyup = totalAmount;可能已经足够了。

然后在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;

和..

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

答案 1 :(得分:1)

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

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

但更好的方法是,允许跨浏览器兼容性是使用addEvent函数:

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

处理事件的更好方法。我建议你改用这种方法。