jQuery将事件附加到实例中的方法 [英] JQuery attach event to method in instance

查看:74
本文介绍了jQuery将事件附加到实例中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有看起来像这样的代码....

I have code that looks like this....

function Finder(id) {
            this.id = id;
            this.input = $("#" + this.id + " :input[type='text']:first")[0];
            $(this.input).bind('keyup'....);



            this.KeyUpHandler = function (e) { ..the event should trigger this.. }

this.input =在'id'中找到类型输入的第一个元素,这就是我要引用的元素.可以满足我的需求.

this.input = the 1st element of type input found within 'id' which is what I will be referencing. This works fine for what I need.

然后我想做的是在'input'上绑定keyup事件.但是,我希望该事件引用函数中包含的实例方法this.KeyUpHandler().

What I want to do then is to bind the keyup event on 'input'. However I want the event to reference the instance method contained in my function - this.KeyUpHandler().

此外,如果我只是在输入标记(onkeypress ="keyuphandler();")上完成此操作,我还需要将'e'传递给函数.

Also I need 'e' to be event that would have been passed into the function had I just done this on the markup for the input (onkeypress="keyuphandler();").

有什么想法可以将事件绑定到我正在使用的函数实例中的a函数吗?

Any ideas how I can bind the event to the a function in the instance of the function I am working within?

推荐答案

function Finder(id) {
  this.id = id;
  this.input = $("#" + this.id + " :input[type='text']:first")[0];
  that=this;
  this.KeyUpHandler = function (e) { ..the event should trigger this.. }
  $(this.input).bind('keyup', this.KeyUpHandler);
}

重要的是,在定义功能后调用bind()

It is important that you call bind() after defining the function!

这篇关于jQuery将事件附加到实例中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆