Вы не можете прикрепить обработчики напрямую к HTML, который не существует
Есть 2 способа справиться с этим.
Свяжите обработчики при успешном обратном вызове ajax.
$(formParentSelector).load(formFileUrl, function() {
/* within this success callback the new html exists and can run code*/
AjaxForm();
});
function AjaxForm(){
jQuery('form.AjaxForm').submit( function() {
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
dataType: 'json',
data : $(this).serialize(),
success : function( data ) {
alert('Submitted');
},
error : function( xhr, err ) {
alert('Error');
}
});
})
}
Другой способ состоит в том, чтобы делегировать обработчик на более высокий уровень в документе, чтобы он был доступен для будущих соответствующих элементов
jQuery(document).on('submit','form.AjaxForm').submit( function() {
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
dataType: 'json',
data : $(this).serialize(),
success : function( data ) {
alert('Submitted');
},
error : function( xhr, err ) {
alert('Error');
}
});
})