添加jquery验证后,单击功能不起作用

时间:2017-08-18 08:47:26

标签: javascript jquery ajax

$(document).on('click', '#dodaj', function(e) {
  $("#addscrap").validate({
    debug: true,
    rules: {
      job: {
        required: true,
        minlength: 3
      },
      keywords: {
        required: true,
        minlength: 5
      }
    },
    messages: {
      job: {
        required: "Podaj nazwę analizy do wykonania",
        minlength: jQuery.validator.format("brakuje {0} znaków!")
      },
      keywords: {
        required: "Podaj słowa kluczowe do analizy",
        minlength: jQuery.validator.format("brakuje {0} znaków!")
      }
    },
    submitHandler: function(form) {
      var data = $("#addscrap").serialize();
      $.ajax({
        data: data,
        type: "post",
        dataType: "JSON",
        url: "views/addscrap.php",
        beforeSend: function(data) {
          document.getElementById('alert').style.display = '';
          $("#alert").text("dodawanie zadania...");
        },
        success: function(data) {
          if (data['error'] == 'exist') {
            document.getElementById('alerterror').style.display = '';
            $('#alerterror').text('Zadanie już istnieje');
            document.getElementById('alert').style.display = 'none';
          } else if (data['error'] == 'add') {
            document.getElementById('alerterror').style.display = 'none';
            $('#alert').text('Dodano zadanie');
            document.getElementById('alert').style.display = '';
            getlist();
          }
        },
        error: function(data) {
          document.getElementById('alerterror').style.display = '';
          $("#alerterror").text("Błąd, spróbuj ponownie");
          document.getElementById('alert').style.display = 'none';
        }
      });
    }
  });
});

当我向这个JS添加jquery验证后,我无法单击按钮#dodaj,当然在控制台中没有错误。有人可以帮我解决这个问题吗?需要在加载函数上添加文档吗?

仅当我删除submitHandler部分时才有效,但是只运行ajax而不对表单进行验证。

           <script>

$( document ).ready(function() {

                $("#addscrap").validate({ 
                      rules: {
                        job: {
                          required: true,
                          minlength: 3
                        },
                        keywords: {
                          required: true,
                          minlength: 5
                        }
                      },
                      messages: {
                        job: {
                          required: "Podaj nazwę analizy do wykonania",
                          minlength: jQuery.validator.format("brakuje {0} znaków!")
                        },
                         keywords: {
                          required: "Podaj słowa kluczowe do analizy",
                          minlength: jQuery.validator.format("brakuje {0} znaków!")
                        }
                      },
                                                                submitHandler: function(form) {
                                           var data = $("#addscrap").serialize();
                                            $.ajax({
                                                data: data,
                                                type: "post",
                                                dataType: "JSON",
                                                url: "views/addscrap.php",
                                                beforeSend: function(data) {
                                                        document.getElementById('alert').style.display = '';
                                                    $("#alert").text("dodawanie zadania...");

                                                },
                                                success: function(data){
                                                if(data['error'] == 'exist') {
                                                    document.getElementById('alerterror').style.display = '';
                                                         $('#alerterror').text('Zadanie już istnieje');
                                                     document.getElementById('alert').style.display = 'none';
                                                }
                                                else if(data['error'] == 'add') {
                                                    document.getElementById('alerterror').style.display = 'none';
                                                         $('#alert').text('Dodano zadanie');
                                                     document.getElementById('alert').style.display = '';
                                                     getlist();
                                                }
                                                },
                                                error: function(data) {
                                                    document.getElementById('alerterror').style.display = '';
                                                    $("#alerterror").text("Błąd, spróbuj ponownie");
                                                    document.getElementById('alert').style.display = 'none';
                                                }
                                            });
                                          }

                       });


});


</script>

0 个答案:

没有答案