开发者

jquery: pass an object to event handler using jquery trigger method

开发者 https://www.devze.com 2023-02-17 03:48 出处:网络
Following code for passing an object to event handler is not working: $(\'a.another\').live(\'click\', function(e, data) {

Following code for passing an object to event handler is not working:

$('a.another').live('click', function(e, data) {
        alert(data); // **alerts '[{isMachineClick:true}]'**
        alert(data.isMachineClick); // **alerts 'undefined'**
});

$('a.another').trigger('c开发者_JAVA百科lick', "[{isMachineClick:true}]");

Please have a look at this.

PS: solution provided at link pass an object through jquery trigger is not working, so posting a new thread.


You're passing only a string, and what's more the JSON inside the string is an array, not an object. Try this:

$('a.another').live('click', function(e, data) {
    alert(data[0].isMachineClick);
});

$('a.another').trigger('click', [{isMachineClick:true}]);

UPDATE: Didn't realize how this worked: using an array is correct, and each additional item becomes another argument. This is the correct code:

$('a.another').live('click', function(e, data, data2) {

    alert(data.isMachineClick);

    alert(data2.someOtherThing);
});

$('a.another').trigger('click', [{isMachineClick:true}, {someOtherThing:false}]);
0

精彩评论

暂无评论...
验证码 换一张
取 消