在JQuery .trigger上传递参数 [英] Passing parameters on JQuery .trigger

查看:165
本文介绍了在JQuery .trigger上传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery触发器,但不确定在我的情况下传递参数的正确语法是什么.这是我打电话的地方:

I am using JQuery trigger but am not sure what the correct syntax is to pass parameters in my situation. Here is where I am making the call :

$('#'+controlName).trigger(event);

在这里,我正在进行事件绑定:

Here is where I am doing the event binding :

$(window).on('onPartialRendered', onPartialRendered);

这是我的事件处理程序:

And here is my event handler :

var onPartialRendered = function () {

    .....
};

一切正常,直到我尝试传递参数.按照我的示例,正​​确的方法是什么?

Everything works fine until I try to pass parameters. What would be the correct way to do it as per my example?

推荐答案

第一个参数始终是带有事件名称的字符串,下一个参数是附加数据:

First parameter is always string with event name and next parameters are additional data:

.trigger('foo', [1, 2]);

.on('foo', function(event, one, two) { ... });

特别感谢Rocket Hazmat

Special thanks for Rocket Hazmat

var controller = {
  listen: function (event, json, string) {}
};

$('body').trigger('this_works', [{data: [1, 2, 3]}, 'something']);

$('body').on('this_works', function (event, json, string) {
  controller.listen(event, json, string);
});

远程部分:

请不要使用这种方式.网络中有很多有关此问题的文章.这会花费很多时间,并且会在网络中产生不必要的流量.请使用这种方式:

Remote partial:

Please do not use this way. There is many article about this problem in network. This take much time and generate unnecessary traffic in network. Please use this way:

var template = $('#templates #example_template').detach();
var clone = template.clone();
clone.find('.some_field').val('new_data');
clone.attr('id', null);
$('table tbody').append(clone);

这篇关于在JQuery .trigger上传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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