在jquery中传递要单击()和bind()事件的参数?

时间:2022-08-26 10:01:10

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

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

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

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

而且如果我们使用bind那么我们将如何做到这一点

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

Please help!

请帮忙!

4 个解决方案

#1


117  

see event.data

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

#2


19  

From where would you get these values? If they're from the button itself, you could just do

你从哪里获得这些价值?如果它们来自按钮本身,你可以这样做

commentbtn.click(function() {
   alert(this.id);
});

If they're a variable in the binding scope, you can access them from without

如果它们是绑定范围中的变量,则可以从外部访问它们

var id = 1;
commentbtn.click(function() {
   alert(id);
});

If they're a variable in the binding scope, that might change before the click is called, you'll need to create a new closure

如果它们是绑定范围中的变量,在调用单击之前可能会发生变化,则需要创建一个新的闭包

for(var i = 0; i < 5; i++) {
   $('#button'+i).click((function(id) {
      return function() {
         alert(id);
      };
   }(i)));
}

#3


0  

var someParam = xxxxxxx;

commentbtn.click(function(){

    alert(someParam );
});

#4


-1  

An alternative for the bind() method.

bind()方法的替代方法。

Use the click() method, do something like this:

使用click()方法,执行以下操作:

commentbtn.click({id: 10, name: "João"}, onClickCommentBtn);

function onClickCommentBtn(event)
{
  alert("Id=" + event.data.id + ", Name = " + event.data.name);
}

Or, if you prefer:

或者,如果您愿意:

commentbtn.click({id: 10, name: "João"},  function (event) {
  alert("Id=" + event.data.id + ", Nome = " + event.data.name);
});

It will show an alert box with the following infos:

它将显示一个警告框,其中包含以下信息:

Id = 10, Name = João

#1


117  

see event.data

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

#2


19  

From where would you get these values? If they're from the button itself, you could just do

你从哪里获得这些价值?如果它们来自按钮本身,你可以这样做

commentbtn.click(function() {
   alert(this.id);
});

If they're a variable in the binding scope, you can access them from without

如果它们是绑定范围中的变量,则可以从外部访问它们

var id = 1;
commentbtn.click(function() {
   alert(id);
});

If they're a variable in the binding scope, that might change before the click is called, you'll need to create a new closure

如果它们是绑定范围中的变量,在调用单击之前可能会发生变化,则需要创建一个新的闭包

for(var i = 0; i < 5; i++) {
   $('#button'+i).click((function(id) {
      return function() {
         alert(id);
      };
   }(i)));
}

#3


0  

var someParam = xxxxxxx;

commentbtn.click(function(){

    alert(someParam );
});

#4


-1  

An alternative for the bind() method.

bind()方法的替代方法。

Use the click() method, do something like this:

使用click()方法,执行以下操作:

commentbtn.click({id: 10, name: "João"}, onClickCommentBtn);

function onClickCommentBtn(event)
{
  alert("Id=" + event.data.id + ", Name = " + event.data.name);
}

Or, if you prefer:

或者,如果您愿意:

commentbtn.click({id: 10, name: "João"},  function (event) {
  alert("Id=" + event.data.id + ", Nome = " + event.data.name);
});

It will show an alert box with the following infos:

它将显示一个警告框,其中包含以下信息:

Id = 10, Name = João