2014-01-19 62 views
0

我已經查看了數據庫,並沒有看到任何意見如何具體適用於我的情況,或者我不是天才,不足以擴展知識。表單驗證器執行兩次

我使用jQuery形式Validor:formvalidator.net

我的頭看起來是這樣的:

<link rel="stylesheet" href="../plugins/uniform/css/uniform.default.css" type="text/css"/> 
<script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script src="../plugins/uniform/jquery.uniform.min.js" type="text/javascript"></script> 
<!--Monitoring Code--> 
<script type="text/javascript" async="async" defer="defer" src="https://chat.banckle.com/chat/visitor.do?dep=72e029a3-64f6-4ffe-b7ea-13c0b3d70a8e"></script> 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.1.38/jquery.form-validator.min.js"></script> 

<script> 
$(function() { 
    $('input,button,select,textarea').uniform(); 
    var myLanguage = { 
     errorTitle : 'Please try again...', 
     requiredFields : 'You have not answered all required fields', 
     badTime : 'You have not given a correct time', 
     badEmail : 'You have not given a correct e-mail address', 
     badTelephone : 'You have not given a correct phone number', 
     badSecurityAnswer : 'You have not given a correct answer to the security question', 
     badDate : 'You have not given a correct date', 
     lengthBadStart : 'You must give an answer between ', 
     lengthBadEnd : ' characters', 
     lengthTooLongStart : 'You have given an answer longer than ', 
     lengthTooShortStart : 'You have given an answer shorter than ', 
     notConfirmed : 'Values could not be confirmed', 
     badDomain : 'Incorrect domain value', 
     badUrl : 'The answer you gave was not a correct URL', 
     badCustomVal : 'You gave an incorrect answer', 
     badInt : 'The answer you gave was not a correct number', 
     badSecurityNumber : 'Your social security number was incorrect', 
     badUKVatAnswer : 'Incorrect UK VAT Number', 
     badStrength : 'The password isn\'t strong enough', 
     badNumberOfSelectedOptionsStart : 'You have to choose at least ', 
     badNumberOfSelectedOptionsEnd : ' answers', 
     badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ', 
     badAlphaNumericExtra: ' and ', 
     wrongFileSize : 'The file you are trying to upload is too large', 
     wrongFileType : 'The file you are trying to upload is of wrong type', 
     groupCheckedRangeStart : 'Please choose between ', 
     groupCheckedTooFewStart : 'Please choose at least ', 
     groupCheckedTooManyStart : 'Please choose a maximum of ', 
     groupCheckedEnd : ' item(s)' 
    }; 
    $.validate({ 
     form: '#contact-form', 
     validateOnBlur: false, 
     errorMessagePosition: 'top', 
     scrollToTopOnError: true, 
     language: myLanguage, 
     onValidate: function() { 
      $('.success-form,.form-error').remove(); 
     }, 
     onSuccess : function() { 
      function processdata(data,success) { 
       if (success) { 
        //document.getElementById('contact-form').reset(); 
        //alert(data); 
        if (data=="good") { 
         $('#contact-form').prepend("<div class='success-form'>Your message has been sent. You will receive a response within the next 24 hours--or on the next business day.</div>"); 
        } else { 
         $('#contact-form').prepend("<div class='form-error'>Unable to contact Genera Safety Inc at this time. Please try again.</div>"); 
        } 
       } 
      } 
      var formdata = $('#contact-form').serialize(); 
      processdata(); 
      //alert(formdata); 
      $.post('process_contact_form.php',formdata,processdata); 
      return false; 
     } 
    }); 
}); 
</script> 

的形式提交了兩次?也許它也驗證兩次?

有人在這裏看到任何問題嗎?我可以在必要時提供進一步的信息......

回答

1

在此處提交事件兩次。我找不到任何代碼問題。看來這個問題是由jquery.uniform插件引起的。您可以使用以下代碼單獨測試插件。

<script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script src="//rawgithub.com/pixelmatrix/uniform/master/jquery.uniform.min.js" type="text/javascript"></script> 

<script> 
$(function(){ 
    $("form").submit(function(e) { 
    e.preventDefault(); 
    console.log("form submit event triggered", Date.now()); 
    }) 

    $('input,button,select,textarea').uniform(); 
}); 
</script> 

<form><input type="submit"/></form> 

通過從您的代碼中刪除$('input,button,select,textarea').uniform();,我能夠避免雙重提交問題。

+1

如果我正確閱讀你的答案,你建議我不應該使用統一插件。這不幸的是不是所需的解決方案。我寧願繼續使用它或功能正常的替代品。然而,我會深入到插件,看看我能否指向那裏。感謝您在尋找錯誤方面的工作! –

+1

我的觀點是突出問題的原因。現在您可以通過自己調試插件或將問題提交給Uniform Plugin社區來關注它。 – Saqib

+0

絕對!謝謝。 –