0

При валидации формы, когда заполнены все поля, проверяемые в selInputs() выскакивает alert дважды. Посдкажите в чем проблема?

var newsForm = $('#add_news_form'),
  themesForm = $('#add_theme_form');

function selInputs(arg) {
  var title = arg.children('.add_mat_title').find('.text'),
    shortDesc = arg.children('.add_mat_short').find('.text'),
    text = arg.children('.add_mat_text').find('.text'),
    source = arg.children('.add_mat_source').find('.text'),
    tags = arg.children('.add_mat_tags').find('.text'),
    inputs = [];

  inputs.push(title, shortDesc, text, source, tags);

  for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].val() == null || inputs[i].val() == '') {
      return false;
    }
  }

  return true;
}

function formValidation(form) {
  $(this).submit(function() {
    if (selInputs($(this)) === false) {
      alert('Заполните все поля!')
      return false;
    }
  });
}

formValidation(newsForm);
formValidation(themesForm);

1 ответ 1

1

Так у Вас функция вызывается дважды. И получается так, что если в обеих формах selInputs($(this)) == false, alert тоже выскакивает дважды. Попробуйте добавть в аргументы каждой формы вот это: onsubmit="formValidation(this)" и убрать все вызовы formValidation внизу скрипта.

Ваш ответ

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

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