Javascript 使用 jQuery Trigger 事件将数据传递给 Change 事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13506209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:13:45  来源:igfitidea点击:

Pass data using jQuery Trigger event to a Change event handler?

javascriptjquery

提问by TaylorMac

Is there a way to pass data to a changeevent via jQuery's triggermethod?

有没有办法change通过 jQuery 的trigger方法将数据传递给事件?

The issue here is that a clickevent triggers the upload menu. Once the image is selected, the changeevent is fired, however. The data I passed into the trigger event's second parameter is passed to the click event, not to the change event. In the example below, datais undefined.

这里的问题是click事件触发了上传菜单。但是,一旦选择了图像,change就会触发该事件。我传递给触发器事件的第二个参数的数据是传递给点击事件,而不是更改事件。在下面的例子中,data是未定义的。

Trigger Image Change

触发图像更改

$('.change_main_image').live('click', function() {
    $('input[name=photo].change_uploader').trigger('click', ["some string"]);
});

Event Handler

事件处理程序

$('input[name=photo].change_uploader').live('change', function (e, data) {
   alert(data); // undefined
   //canvas resize and upload script
});

回答by thecodeHyman

.live()is deprecated. Use .on()like this. And changeevent should be triggered, not click

.live()已弃用。.on()像这样使用。而change事件应该被触发,不click

$('.change_main_image').on('click', function() {
    $('input[name=photo].change_uploader').trigger('change', [{somedata:true}]);
});


$('input[name=photo].change_uploader').on('change', function (e, data) {
   alert(data.somedata); 
   //canvas resize and upload script
});