Not getting jQuery.extend options correctly inside an event

Not getting jQuery.extend options correctly inside an event

I'm trying to create a plugin with some form processing & i need to call a success function if form submitted correctly. I used  $.extend to extend the options. 

  1. function Foo(elem,options){
  2. var self = this;
  3. $elem = $(elem);
  4. settings = $.extend({}, $.fn.test.defaults, options);
  5. self.init();
  6. }

  7. Foo.prototype.init = function(){
  8. $elem.on('click', function(e){
  9. e.preventDefault();
  10. if(typeof settings.onSuccess === 'function' ){
  11. settings.onSuccess.call(this);
  12. }
  13. });
  14. }

  15. $.fn.test = function(options) {
  16.     
  17. return this.each(function(){
  18. var element = $(this);
  19. var form = new Foo(this, options);
  20. });

  21. };

  22. $.fn.test.defaults = {
  23. onSuccess : null
  24. };
i created two forms and called two different functions for these form onSuccess .

  1. <form id="form1">
  2. <input type="text" name="input1" />
  3.     <input type="submit" name="submit1" />
  4. </form>

  5. <form id="form2">
  6. <input type="text" name="input2" />
  7.     <input type="submit" name="submit2" />
  8. </form>

  9. (function($){
  10. $('#form1').test({
  11. onSuccess : function(){alert('form1 submitted');}
  12. });
  13. $('#form2').test({
  14. onSuccess : function(){alert('form2 submitted');}
  15. });
  16. })(jQuery);

but while submitting both forms only executes the last extended function. ie, alert ('form2 submitted'). 

but when calling outside of submit function, it works....

Please anyone help me to findout what's the actual problem...