Passing parameters on JQuery .trigger

后端 未结 1 1000
無奈伤痛
無奈伤痛 2020-12-13 03:09

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 :

$(\'#\'+contro         


        
1条回答
  •  無奈伤痛
    2020-12-13 03:45

    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

    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);
    

    0 讨论(0)
提交回复
热议问题