在 JQuery .trigger 上传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16401538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameters on JQuery .trigger
提问by user517406
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 :
我正在使用 JQuery 触发器,但不确定在我的情况下传递参数的正确语法是什么。这是我打电话的地方:
$('#'+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?
一切正常,直到我尝试传递参数。根据我的示例,正确的方法是什么?
回答by Eraden
First parameter is always string with event name and next parameters are additional data:
第一个参数始终是带有事件名称的字符串,下一个参数是附加数据:
.trigger('foo', [1, 2]);
.on('foo', function(event, one, two) { ... });
Special thanks for Rocket Hazmat
特别感谢 Rocket Hazmat
Example:
例子:
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);