0

как сделать чтобы форма отправлялась с первого раза? И по возможности. чтобы валидация проводилась все время? Валидация нужна только на offer-form. дальше разберусь. Я новичок. Но задания нужно выполнить. Прошу помогите.

    $(document).ready(function () {
  var close = $('#close2');
  var form = $('.form');
  var success_message = $('.none');
  var time_animation = 500;
  var time_return = 3000;
$("#brif-form").validate({
    rules: {
      username: {
        required: true,
        minlength: 2,
        maxlength: 15
      },
      email: {
        required: true,
        email: true
      },
      phone: {
        required: true
      }
    },
    messages: {
      username: {
        required: "Укажите ваше имя",
        minlength: jQuery.validator.format("Осталось символов - 1"),
        maxlength: "максимальное число символов - 15"
      },
      email: "Укажите корректный email адрес",
      phone: "Укажите номер телефона",
    },
    errorClass: "invalid",
    errorElement: "div",
  });
$("#modal-form").validate({
  rules: {
    username: {
      required: true,
      minlength: 2,
      maxlength: 15
    },
    phone: {
      required: true
    }
  },
  messages: {
    username: {
      required: "Укажите ваше имя",
      minlength: jQuery.validator.format("Осталось символов - 1"),
      maxlength: "Максимальное число символов - 15"
    },
    phone: {
      required: "Укажите номер телефона"
    }
  },
  errorClass: "invalid",
  errorElement: "div",
});
$("#offer-form").validate({
  rules: {
    username: {
      required: true,
      minlength: 2,
      maxlength: 15
    },
    phone: {
      required: true
    }
  },
  messages: {
    username: {
      required: "Укажите ваше имя",
      minlength: jQuery.validator.format("осталось символов - 1"),
      maxlength: "Максимальное число символов - 15"
    },
    phone: "Укажите номер телефона"
  },
  submitHandler: function () {
    $('#offer-form').on('submit', function (event) {
      event.preventDefault();
      $.ajax({
        url: 'mail.php',
        type: 'POST',
        data: $(this).serialize(),
        success: function (data) {
          form.hide(time_animation);
          $('form :input').val('');
          success_message.show(time_animation);
          setTimeout(function () {
            form.show(time_animation);
            success_message.hide(time_animation);
          }, time_return);
        }
      });
    });
  },
  errorClass: "invalid",
  errorElement: "div",
});
});
3
  • Полагаю, это из-за "сабмита в сабмите"... submitHandler: function (e) { аналог той строки что у вас ниже - $('#offer-form').on('submit', function (event) {... просто удалите вторую строку, она лишняя
    – InDevX
    11 авг 2019 в 18:08
  • да. еще нужно было поменять $(this) на $('#offer-form') так как "this" уже код не видет
    – Артем
    11 авг 2019 в 18:16
  • submitHandler: function (form,event) { и не нужен this, и потом data: $(form).serialize(),
    – InDevX
    11 авг 2019 в 18:25

1 ответ 1

0

решил.

    submitHandler: function () {
    /* $('#offer-form').on('submit', function (event) { */
      event.preventDefault();
      $.ajax({
        url: 'mail.php',
        type: 'POST',
        data: $('#offer-form').serialize(),
        success: function (data) {
          form.hide(time_animation);
          $('form :input').val('');
          success_message.show(time_animation);
          setTimeout(function () {
            form.show(time_animation);
            success_message.hide(time_animation);
          }, time_return);
        }
      /* }); */
    });

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.