开发者

Capture keycodes inside prototype function

I'm trying to capture keystrokes inside my prototype functio开发者_如何转开发n.

Here's my code:

function txtBox(input) // pass textbox
{
     this.id = "myTextbox";
     this.txt = input
}
txtBox.prototype.init = function()
{
     this.txt.bind("keyup",this.keyup);
}
txtBox.prototype.keyup= function(event)
{
     alert("keycode: event.keyCode);
     alert(this.id);
}
var myTxt = new txtBox($(#txt)); // create object
myTxt.init();

Capturing works but the problem is that the keyup triggers "outside" my object, which means this.id returns "undefined" even though it was defined.

Does anyone know how to keep consistency with this?


I tried copy pasting your code into my firebug and running it on this page (changed the alerts to console.log though). And it seems to work just fine. It captures every keydown in the textfield I selected, but not anywhere else.


Two problems I see, assuming you have an actual tag named "txt" in your script as so:

<input type="text" id="txt"/>

this line needs to be change from

alert("keycode: event.keyCode);

to

alert("keycode": event.keyCode);

also this needs to be changed from

new txtBox($(#txt)); 

to

new txtBox($('#txt')); 

and it will alert your textbox name, I dont believe in IE you will be able to change the id the way you're trying to change it doing this.id="textbox"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜