带参数的 jQuery 绑定和取消绑定事件

发布于 2024-08-11 06:51:20 字数 238 浏览 6 评论 0原文

我正在尝试将事件绑定到包含参数的文本框。下面的 keep 看起来好像应该这样做,但每次页面加载时,它都会被执行。

jQuery(function(){
    jQuery('#textbox').bind('click', EventWithParam('param'));
});

每次加载页面时都会使用该参数调用该事件。这可能不起作用,因为不支持带参数的事件。如果是的话,还有其他路线吗?

I am trying to bind an event to a textbox that contains parameters. The following keep looks as if it should do it, but every time the page loads, it gets executed.

jQuery(function(){
    jQuery('#textbox').bind('click', EventWithParam('param'));
});

The event gets called with that parameter every time the page loads. This may not work because events with parameters are not supported. If so, is there another route?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少跟Wǒ拽 2024-08-18 06:51:20

您可以将事件参数作为第二个参数传递给bind()。它不是直接回调参数,但也许您想使用它:

来自 jQuery 文档:
绑定

function handler(event) {
    alert(event.data.foo);
}  
$("p").bind("click", {foo: "bar"}, handler)

You can pass event parameters as second arguments to bind(). It is not direct callback parametes but maybe you would like to use it:

From jQuery documentation:
bind

function handler(event) {
    alert(event.data.foo);
}  
$("p").bind("click", {foo: "bar"}, handler)
海的爱人是光 2024-08-18 06:51:20

要绑定接受参数的函数,请使用匿名函数作为参数的闭包。

jQuery(function($) {
    var param = 'text';
    $('#textbox').click(function() {
        EventWithParam(param);
        // or just use it without calling out to another function
        $(this).text(param);
    });
});

您的示例正在执行 EventWithParam 函数,然后尝试绑定到该函数调用的结果。

调用 unbind 而不指定函数将取消绑定指定类型的所有处理程序事件(包括匿名函数)。如果您想专门取消绑定该函数,则需要为其提供一个名称,如下所示:

jQuery(function($) {
    var param = 'text',
        clickCallback = function() {
            EventWithParam(param);
            // or just use it without calling out to another function
            $(this).text(param);
        };
    $('#textbox').click(clickCallback);
});

To bind a function that takes parameters, use an anonymous function to act as a closure over the parameters.

jQuery(function($) {
    var param = 'text';
    $('#textbox').click(function() {
        EventWithParam(param);
        // or just use it without calling out to another function
        $(this).text(param);
    });
});

Your example is executing the EventWithParam function, and then trying to bind to the result of that function call.

Calling unbind without specifying a function will unbind all handlers for the specified type of event (including the anonymous function). If you want to unbind that function specifically, you'll need to provide it with a name, like this:

jQuery(function($) {
    var param = 'text',
        clickCallback = function() {
            EventWithParam(param);
            // or just use it without calling out to another function
            $(this).text(param);
        };
    $('#textbox').click(clickCallback);
});
北城半夏 2024-08-18 06:51:20

bdukes 是完全正确的。我添加我的响应是因为我最近发现了一些与 jQuery 的绑定/取消绑定函数和匿名函数及其事件名称空间有关的有趣内容。如上所示,绑定事件的调用方式如下。

jQuery('#textbox').bind('click',function(){EventWithParam('param')});

要取消绑定,即使用户也必须调用 ...
jQuery('#textbox').unbind('click');

实际上,删除所有 onClick 事件。

现在出现在 jQuery 命名空间中。如果您有一个匿名函数,您希望附加到单击事件并稍后取消绑定,请按如下操作。

jQuery('#textbox').bind('click.yourNameSpace',function(){EventWithParam('param')});

然后要解除绑定,只需调用解除绑定函数,如下所示。

jQuery('#textbox').unbind('click.yourNameSpace');

其余的 onClick 事件将保留。

bdukes is exactly right. I'm adding my response as I've recently found something interesting having to do with jQuery's bind/unbind function and anonymous functions, its event name spaces. Previously shown above the bind event is called as follows.

jQuery('#textbox').bind('click',function(){EventWithParam('param')});

To unbind this even the user must call ...
jQuery('#textbox').unbind('click');

In effect, removing all onClick events.

Now comes in jQuery name spaces. If you have an anonymous function you wish to attach to the click event and later unbind do it as follows.

jQuery('#textbox').bind('click.yourNameSpace',function(){EventWithParam('param')});

Then to unbind simple call the unbind function as follows.

jQuery('#textbox').unbind('click.yourNameSpace');

The rest of your onClick events will remain.

春夜浅 2024-08-18 06:51:20

原因是 EventWithParam 在绑定中作为函数被调用。换句话说,您将点击事件绑定到调用 EventWithParam('param')结果。这应该可行:

jQuery('#textbox').bind('click',function(){EventWithParam('param')});

现在您已将 Even 绑定到一个函数,当事件触发时,该函数将调用 EventWithParam('param')

The reason is that EventWithParam is being called as a function right there in your binding. Phrased another way, you're binding the click event to the result of calling EventWithParam('param'). This ought to work:

jQuery('#textbox').bind('click',function(){EventWithParam('param')});

Now you've bound the even to a function that, when the event fires, will call EventWithParam('param')

谜泪 2024-08-18 06:51:20
jQuery(function() {
    jQuery('#textbox').click(function() {
        EventWithParam(param1,param2,...,paramN);
    });
});

function EventWithParam(param1,param2,...,paramN) {
    doSomething...;
    ...;
}
jQuery(function() {
    jQuery('#textbox').click(function() {
        EventWithParam(param1,param2,...,paramN);
    });
});

function EventWithParam(param1,param2,...,paramN) {
    doSomething...;
    ...;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文