传递参数click()& jQuery中的bind()事件? [英] Passing parameters to click() & bind() event in jquery?

查看:91
本文介绍了传递参数click()& jQuery中的bind()事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jquery中传递几个参数给Click()事件,我尝试以下但不起作用,

I want to pass few parameters to Click() event in jquery, I tried following but its not working,

commentbtn.click(function(id, name){
    alert(id);
});

如果我们使用bind,那么我们该怎么做那个

And also if we use bind then how we'll do that

commentbtn.bind('click', function(id, name){
    alert(id);
});

请帮助!

推荐答案

请参阅 event.data

commentbtn.bind('click', { id: '12', name: 'Chuck Norris' }, function(event) {
    var data = event.data;
    alert(data.id);
    alert(data.name);
});

如果您的数据在绑定事件之前被初始化,那么只需在闭包中捕获这些变量。 p>

If your data is initialized before binding the event, then simply capture those variables in a closure.

// assuming id and name are defined in this scope
commentBtn.click(function() {
    alert(id), alert(name);
});

这篇关于传递参数click()& jQuery中的bind()事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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