JQuery отправка нон-стоп - бесконечный цикл - PullRequest
0 голосов
/ 08 февраля 2019

У меня есть этот jQuery, который проверяет, похоже ли значение текстового поля (id_code) на любое из доступных значений в поле выбора (id_tipAux).

Пользователь не должен отправлятьзначение в текстовом вводе (код), подобное значению, которое уже существует в поле выбора (типы).

Я использую SweetAlert.

Вот мой код jQuery:

$('#frmCreateNew').submit(function(e) {
    e.preventDefault(); // I prevent the post to submit before checking it
    var codeExists = false;
    var types = document.getElementById('id_tipAux');
    var code = document.getElementById('id_code').value;
    code = code.toUpperCase(); //select vals are all uppercase
    console.log('Input : ' + code +);
    var i;
    for (i = 0; i < types.length; i++){ // iterate all the values of the select field
        console.log(types.options[i].value);
        if(code == type.options[i].value){ //checks if any of them is equal to the text input value
            codeExists = true; //sets the var to true in order to prevent a submit
            swal("The code already exists.", "Please type another code", "error");
        }
    }
    if(codeExists == false) { //var is false so it allows to submit
        swal("New type created!", {icon: "success",})
        .then((value) => {
            console.log(value)
            $(this).submit() // the form is submitted and the infinite loop starts here
        });
    }
});

С помощью этого jQuery я попытался остановить отправку, если пользователь отправляет текстовый ввод, равный любому из параметров поля выбора.И это сработало, но проблема возникает, когда пользователь отправляет принятое значение, он запускает бесконечный цикл из-за $this.submit(), так как метод ожидает отправки для frmCreateNew

1 Ответ

0 голосов
/ 08 февраля 2019

Вы должны вызывать отправку собственной формы, а не jQuery-оболочки:

$('#frmCreateNew').submit(function (e) {
  e.preventDefault();
  var codeExists = false;
  var types = document.getElementById('id_tipAux');
  var code = document.getElementById('id_code').value;
  code = code.toUpperCase(); //select vals are all uppercase
  console.log('Input : ' + code + );
  var i;
  for (i = 0; i < types.length; i++) {
    console.log(types.options[i].value);
    if (code == type.options[i].value) {
      codeExists = true; //sets the var to true in order to prevent a submit
      swal("The code already exists.", "Please type another code", "error");
    }
  }
  if (codeExists == false) { //var is false so it allows to submit
    swal("New type created!", {
      icon: "success",
    })
    .then((value) => {
      console.log(value)
      //$(this).submit() // the form is submitted and the infinity loop starts here

      // Do this
      this.submit();
    });
  }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...