jquery .on()通过函数传递数据 [英] jquery .on() pass data through function

查看:226
本文介绍了jquery .on()通过函数传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读.on()jquery文档。

I am reading the .on() jquery documentation.

是否可以传递这样的数据:

And is it possible to pass some data like this:

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

该方法的原型是:

$(selector).on(event,childSelector,data,function,map)

是否可以在数据参数中使用更复杂的瘦而不是只有一个字符串?。

Is it possible to have a more complex thin instead of only a string into the data parameter?.

在我的情况下:
I将动态生成一些带有数字输入和一些文本的弹出窗口。

In my case: I will be generating dynamically some popups with a number input inside and some text.

是否可以捕获调用者弹出窗口内的信息,即数字和文本来电弹出窗口。
也许是这样的:

Is it possible to capture the information inside the caller popup, i.e. the number and the text of the caller popup. Maybe something like this:

$(document).on('click', '#id', 
var data =[
var number = functionThatGetTheNumberOfThePopup
var test = captureTextOfPopup
] , 
function(event){
//use number
//use test
});


推荐答案

是的,您可以传递任意数据。你以对象的形式这样做:

Yes, you can pass arbitrary data. You do it in the form of an object:

$(document).on('click', '#id', {
        number: functionThatGetTheNumberOfThePopup,
        test: captureTextOfPopup
    },
    function(event){
    //use number
    console.log(event.data.number);
    //use test
    console.log(event.data.test);
});

这篇关于jquery .on()通过函数传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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