Passing parameters to click() & bind() event in jquery?

后端 未结 4 1383
傲寒
傲寒 2020-12-04 10:10

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);
});
         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 10:45

    see event.data

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

    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);
    });
    

提交回复
热议问题