Hello, please help understand and understand the following example. I need an explanation of this code, it is advisable to comment line by line ...

$(function(){ $('#my_form').on('submit', function(e){ e.preventDefault(); var $that = $(this), fData = $that.serialize(); .... // fData = $that.serializeArray(); $.ajax({ url: $that.attr('action'), type: $that.attr('method'), data: {form_data: fData}, dataType: 'json', success: function(json){ if(json){ $that.replaceWith(json); } } }); }); }); 

    1 answer 1

     $(function(){ // Короткая запись $(document).on('ready'.. данная функция выполняется //когда страница/документ готов к работе //на элемент с id="my_form" вешаем событие submit $('#my_form').on('submit', function(e){ //Отключаем действие по умолчанию, без этой строчки форма будет отправляться без ajax e.preventDefault(); //Присвоение переменной $that. Про this лучше прочитать получше, очень грубо //говоря все что есть у элемента, передастся в эту переменную this var $that = $(this); //fData будет равен строке в формате serialize ГЫ fData = $that.serialize(); //В этой строчке fData будет равна массиву всех аргументов формы // fData = $that.serializeArray(); //Готовим ajax запрос $.ajax({ //У формы есть атрибут action, берем его и подставляем в адрес куда будет послан запрос url: $that.attr('action'), //У формы есть атрибут method в основном это POST,GET подставляем его type: $that.attr('method'), //Передаем данные из формы в формате serilize data: {form_data: fData}, //Указываем что формат будет json dataType: 'json', //При успешном ответе принимаем значение json success: function(json){ //Если данные есть, то... if(json){ //пытаемся заменить $that тем что получили $that.replaceWith(json); } } }); }); }); 
    • no comment for line with .... - Grundy
    • // IT IS MAGIC, would that fit? - lazyproger
    • Is this magic? :) - Grundy
    • There are such codes, where everything seems to be ok, but the author points out more ... And that no one there knows, but why then put these ... secret vulture? I'm inclined to believe that there is magic - lazyproger
    • // Change the data ? exactly? - Grundy