传递参数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);
});