Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

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

Is there a way to pass data to a change event via jQuery's trigger method?

The issue here is that a click event triggers the upload menu. Once the image is selected, the change event 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, data is undefined.

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
});
question from:https://stackoverflow.com/questions/13506209/pass-data-using-jquery-trigger-event-to-a-change-event-handler

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.live() is deprecated. Use .on() like this. And change event should be triggered, not 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
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...