-
-
Save igorpronin/4298499213e630037735 to your computer and use it in GitHub Desktop.
Аяксовая отправка формы + попап на плагине Magnific Popup + блокировка формы во время работы Аякса + функция сброса формы
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Объявление попап-функций. Плагин Magnific Popup | |
var openSucsessPopup = function() { | |
$.magnificPopup.open({ | |
items: { src: '#sucsess-popup' }, | |
type: 'inline', | |
fixedContentPos: false, | |
fixedBgPos: true, | |
overflowY: 'auto', | |
closeBtnInside: true, | |
preloader: false, | |
midClick: true, | |
removalDelay: 300, | |
mainClass: 'my-mfp-zoom-in' | |
}); | |
}; | |
var openFailPopup = function() { | |
$.magnificPopup.open({ | |
items: { src: '#fail-popup' }, | |
type: 'inline', | |
fixedContentPos: false, | |
fixedBgPos: true, | |
overflowY: 'auto', | |
closeBtnInside: true, | |
preloader: false, | |
midClick: true, | |
removalDelay: 300, | |
mainClass: 'my-mfp-zoom-in' | |
}); | |
}; | |
// Функция сброса формы. | |
var resetForm = function() { | |
$('#my-form').trigger('reset'); | |
}; | |
// Функции отслеживания событий | |
var submitListener = function() { | |
$('#my-form').on("submit", sendAjax); | |
}; | |
// Аяксовая отправка форм. | |
var sendAjax = function(event) { | |
event.preventDefault(); | |
var submitButton = $(this).find('input[type="submit"]'); | |
submitButton.attr('disabled', ''); | |
var | |
form = $(this), | |
url = form.attr('action'), | |
data = form.serialize(), | |
result = $.ajax({ | |
url: url, | |
type: 'POST', | |
dataType: 'json', | |
data: data | |
}) | |
.done(function() { | |
console.log("sucsess"); | |
openSucsessPopup(); | |
resetForm(); | |
}) | |
.fail(function() { | |
console.log("error"); | |
openFailPopup(); | |
}) | |
.always(function() { | |
submitButton.removeAttr('disabled'); | |
}); | |
}; | |
// Инилизация функций. | |
submitListener(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment