Javascript event doesn't seem to get added to

2019-07-17 03:50发布

问题:

I added onkeyup javascript for a dynamically added textbox in javascript... But it doesnt seem to work....

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

But when i used

e3.onkeyup = totalAmount;

It worked... Here is my javascript function,

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

    }

}

}

回答1:

You need to wrap your function call in an anonymous function:

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

But an even better way to do it, to allow for cross browser compatibility would be to use an addEvent function:

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

And then add the event using that function:

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

Just a much better way to handle events. I would recommend you switch to this method.



回答2:

onkeyup is a function. If you pass it the return value of totalAmount(event,this,'tblsample'); it won't work (unless it returns a function).

e3.onkeyup = totalAmount; is probably enough.

then inside totalAmount..

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

So if you need the this and the 'tblsample' arguments, I suggest you add them to the e3 object so that you can access them through the this keyword inside the totalAmount function:

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

and..

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

Or you can simply just do

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