简体   繁体   English

在parsley.js验证后提交Ajax

[英]Ajax submit after parsley.js validation

I have a form that submits through ajax. 我有一个通过ajax提交的表单。 Works fine. 工作良好。 However, I am trying to work out how to ONLY submit it if parsley successfully verifies the form. 但是,如果parsley成功验证表单,我正在尝试解决如何提交它。

The issue is that the form submits even if parsley displays an issue. 问题是即使欧芹显示问题,表单也会提交。 How can I allow for this? 我怎么能允许这个?

<script>
  $(document).ready(function(){
  $('#newsletterSubscribe').on('submit',function(e) {

  $.ajax({
    url:'scripts/newsletter.php',
    data:$(this).serialize(),
    type:'POST',
    success:function(data){
    console.log(data);
    $("#success").show().fadeOut(20000); //=== Show Success Message==
    },
    error:function(data){
    $("#error").show().fadeOut(20000); //===Show Error Message====
    }
  }); 
  e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
  });
  });
</script> 

You need to add the Parsley validation condition, as following: 您需要添加Parsley验证条件,如下所示:

<script>
  $(document).ready(function(){
  $('#newsletterSubscribe').on('submit',function(e) {
  e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
  if ( $(this).parsley().isValid() ) {
     $.ajax({
       url:'scripts/newsletter.php',
       data:$(this).serialize(),
       type:'POST',
       success:function(data){
       console.log(data);
         $("#success").show().fadeOut(20000); //=== Show Success Message==
       },
       error:function(data){
         $("#error").show().fadeOut(20000); //===Show Error Message====
       }
     }); 
   }
  });
  });
</script> 

Note also that the Prevent Default behavior is set as a first step in the process. 另请注意,“防止默认”行为设置为该过程的第一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM