简体   繁体   English

如何在Jquery中将参数传递给回调函数?

[英]How to Pass parameters to callback functions in Jquery?

I have a function like this 我有这样的功能

$("#button1").on("click", clickEvent);

function clickEvent(someParameter){
  console.log(someParameter);
}

How Can I pass some value (for ex: 1) to clickEvent function in that situation ? 在那种情况下,如何将某些值(例如:1)传递给clickEvent函数?

From the jQuery API Documentation : 来自jQuery API文档

If a data argument is provided to .on() and is not null or undefined, it is passed to the handler in the event.data property each time an event is triggered. 如果向.on()提供了数据参数,并且该参数不为null或未定义,则每次触发事件时都会将其传递给event.data属性中的处理程序。 The data argument can be any type, but if a string is used the selector must either be provided or explicitly passed as null so that the data is not mistaken for a selector. data参数可以是任何类型,但如果使用字符串,则必须提供选择器或将其显式传递为null,以便数据不会被误认为是选择器。 Best practice is to use a plain object so that multiple values can be passed as properties. 最佳做法是使用普通对象,以便可以将多个值作为属性传递。

function greet( event ) {
  alert( "Hello " + event.data.name );
}
$( "button" ).on( "click", {
  name: "Karl"
}, greet );
$( "button" ).on( "click", {
  name: "Addy"
}, greet );

The arguments are always the event, but you can use a closure to get more data into your callback 参数始终是事件,但您可以使用闭包来获取更多数据到您的回调中

$("#button1").on("click", function(){clickEvent(1);});

function clickEvent(someParameter){
  console.log(someParameter);
}

You don't control which parameters get passed. 您无法控制传递哪些参数。 It's always the event object which gets passed into the handler function. 它始终是传递给处理函数的事件对象。

You can also use Function.prototype.bind , but you'll lose the this argument: 您也可以使用Function.prototype.bind ,但是您将丢失this参数:

$("#button1").on("click", clickEvent.bind(null, 1));

function clickEvent(someParameter){
    console.log(someParameter);
    // in here, `this` no longer refers to the button
}

Or use closures in a slightly different way: 或者以稍微不同的方式使用闭包:

$("#button1").on("click", makeClickEventHandler(1));

function makeClickEventHandler(someParameter){
    return function(event) {
        console.log(someParameter);
    }
}

But tobyodavies' answer is the most applicable solution here. 但是Tobyodavies的答案是最合适的解决方案。

if the parameters are all like your example primitive and constant you can simply do this: 如果参数都像你的示例原语和常量,你可以简单地这样做:

function addClickEvent(param){
    var clickEvent=Function('var param = "'+param+'"; alert(param);');
    $("#button1").on("click", clickEvent);
}
addClickEvent(1);

but it doesn't mean if your param is kinda object you can't do this. 但这并不意味着如果你的参数是有点对象你不能这样做。 just the solution would differe, like: 只是解决方案会有所不同,例如:

function addClickEvent(param){
    window._allParams=[];
    var pindex =_allParams.push(param) - 1;
    var clickEvent=Function('var param =window._allParams['+pindex+']; alert(param);');
    $("#button1").on("click", clickEvent);
}

You can try this: 你可以试试这个:

(function() {

 var args = arguments;

 function clickEvent(){
  return function(e) {
     return args; // [1,2,3,4];
  }();
 }

 keys.on('click',clickEvent);

})(1,2,3,4);

just pass value where you are calling it 只需传递您调用它的值

$("#button1").on("click", function(){clickEvent("1");});

function clickEvent(someParameter){
  console.log(someParameter);
}

when you need to pass value through function, you must pass while calling it, where you are already defining it at "someParameter" that you may pass some values to function. 当你需要通过函数传递值时,你必须在调用它时传递,你已经在“someParameter”中定义它,你可以将一些值传递给函数。 "someParameter" will act as variable inside function. “someParameter”将充当函数内部的变量。

you can also use this function like this 你也可以像这样使用这个功能

var somevar = "1";

$("#button1").on("click", function(){clickEvent(somevar);});

function clickEvent(someParameter){
console.log(someParameter);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM