jQuery: How do I chain .live() with .ajaxForm()?
I have a form that I pull in via XHR, so it isn't ready when the DOM is initially rendered. Currently I use something like:
jQuery("[name='myform']").ajaxForm({ /* code here */ });
and it works fine on a stand alone app. The app I'm embedding it in is huge, and I need to be able to bind ajaxForm to myform before myform is available to the DOM. Is there anyway I can combined .live() here in order to have jQuery watch for it when it is brought in via XHR?
** EDIT **
here's the actual code that's being executed. no javascript errors. It just does a full page submit instead of an XHR update.
jQuery("[name='clip_form2']").live('submit', function(e) {
$(this).ajaxSubmit({
target: '#form_quotes_highlights_part',
beforeSerialize: function(form, options) {
alert("In beforeSerialize...");
if (validate_time_text_highlights()) {
if ( $tabChanged ) {
diff(form[0]);
jQuery('form[name=clip_form2] input[type=submit]').attr('disabled', 'disabled').val("<%= t('labels.please_wait') %>");
return true;
}
else {
return false;
}
}
return false;
},
success: function() {
jQuery('#form_quotes_highlights_part').fadeIn('slow');
},
complete: function() {
jQuery("#wizard").expose().close();
}
});
$tabChanged = false;
add_change_listener("form[name=clip_form2]");
Tabs.validateCancel( $( "button[name=Cancel]", "form[name开发者_如何学Python=clip_form2]" ) );
$("#clip_quote").NobleCount('#quote_count');
$("#clip_quote2").NobleCount('#quote2_count');
$("#clip_attribution").NobleCount('#attribution_count');
if ( <%= is_defined?( @permitted_clip_read_only ) && @permitted_clip_read_only %> ) {
jQuery( 'form[name=clip_form2] input' ).attr( "disabled", true );
jQuery( 'form[name=clip_form2] textarea' ).attr( "disabled", true );
}
e.preventDefault();
});
Something like this should do the trick:
jQuery("[name='myform']").live('submit', function(e) {
$(this).ajaxSubmit(/* code here */);
e.preventDefault();
});
ajaxSubmit()
is where the magic happens (where as ajaxForm()
just binds a submit handler), here we're just listening for the submit
event and calling ajaxSubmit()
then.
精彩评论