开发者

Overriding form submit event with jQuery (1.3.2)

I cannot find a solution for that, I need to override a previously set submit event handler. In my example, I have a iframe page, with their own submit (which can have validation errors, and return false). So, I'm need to do:

$('<iframe></iframe>')
    .load(function() {
       content = $(this).contents()
       form = content.find('form')
       previous_form_submit = .... //This is my question
       form.unbind("submit")
       form.submit(function() {
           var valid = previous_form_submit()
           if (valid) {
             $.post(form.attr('action'), form.serialize(), function(data) {
                //do something
            })
        })
     })

Edit: To be more clearer, the problem is override the event, but without losing previ开发者_如何学运维ous handler with validation.

Edit2: I'm using jQuery 1.3.2

I'm going to another kind of solution, but I felt curious about how can do that.


Event handlers are stored in the element's expando data store, accessible by .data(), in this case, .data('events') for event handlers, then you do .submit for bound submit handlers, .click for clicks, etc.


jQuery 1.4+ version:

If the form had one submit handler it would look like this:

var oldHandler = form​​​​​​​​.data("events").submit[0].handler;

Otherwise you need to loop through them, using a $.each() for example:

$.each(form.data("events").submit, function(i, h) {    
  var handler = h.handler;
  //do something with handler
});

jQuery 1.3.x version:

Using a $.each() for example:

$.each(form.data("events").submit, function(i, h) {    
  var handler = h;
  //do something with handler
});

It's best to loop though here, as the indexes aren't cleaned up. A handler may not be the first item, so .submit[0] might be undefined, even if there is a handler.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜