I have some code that looks like the following:
jQuery("[name='myform']").live('submit', function() {
$(this).ajaxForm({
target: '#someform',
beforeSerialize: function(form, options) {
alert("BeforeSerialize happening.");
jQuery('form[name=myform] input[type=submit]').attr('disabled', 'disabled').attr("value", "<%= 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();
}
});
});
The live() binding seems to work just fine, however the ajaxForm is not working at all. If I do something like:
$(document).ready(function() {
/* ajax form stuff here */
});
It appears to work just fine. The only problem with that is, when the response comes back from the server, javascript handlers are not attaching themselves to the DOM properly, hence the use of live(). Either way, beforeSerialize is never invoked with the way I'm currently handling my live() handler, so instead of an ajaxForm, I'm having a full page submit.
What's the best way to cle开发者_Go百科an this up?
I never used the ajaxForm
plugin, but looking at its documentation, it looks like the call to ajaxForm
simply sets up the form, it doesn't actually submit the form. So when your function that is bound to the form's submit function is called, the form is already being submitted, and thus it is too late to set up the form as an AJAX form via ajaxForm
.
If I understand correctly, the myform
form doesn't exist on the page when the page is loaded; instead, it's being inserted into the page by an AJAX call at some point. The success
callback of that AJAX call is where you should be calling ajaxForm
as well in order to prep the form ahead of time for when the user submits it. e.g.:
$.get('/get_my_form', function(data) {
$('form[name="myform"]').html(data).ajaxForm({
target: '#someform',
beforeSerialize: function(form, options) {
$('form[name=myform] input[type=submit]').attr('disabled', 'disabled').attr("value", "<%= t('labels.please_wait') %>");
return false;
},
success: function() {
jQuery('#form_quotes_highlights_part').fadeIn('slow');
},
complete: function() {
jQuery("#wizard").expose().close();
}
});
});
精彩评论