2016-05-04 56 views
0

我已經自定義了我的表單來驗證必填字段,但即使所有字段都填寫正確,它也不會提交。從我的理解,jquery.validate.min.js阻止它被提交。有誰知道爲什麼我的表單不提交?Jquery驗證表單不發帖

的代碼如下:

<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
    <script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script> 
    <script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script> 
    <link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css"> 
</head> 


<div> 
<form id="myform" method="POST" action=""> 

<label for="fname">First Name *: </label> 
<input id="fname" name="fname"> 
<br/> 
<label for="lname">Last Name *: </label> 
<input id="lname" name="lname"> 
<br/> 
<label for="email">Email *: </label> 
<input id="email" name="email"> 
<br/> 
<label for="jtitle">Job Title *: </label> 
<input id="jtitle" name="jtitle"> 
<br/> 
<label for="orgname">Organization *: </label> 
<input id="orgname" name="orgname"> 
<br/> 
<label for="orgname">Describe Yourself *: </label> 
<input id="desyourself" name="desyourself" type="hidden" value="123"> 
<select id="desyourself" name="desyourself"> 
        <option value=""> 
        Choose One...  </option> 
        <option value="124"> 
        Customer 
        </option> 
        <option value="125"> 
        Partner 
        </option> 
        <option value="126"> 
        Prospect 
        </option> 
        <option value="127"> 
        Other 
        </option> 
        <option value="128"> 
        Misc. 
        </option> 
</select> 
<br/> 
<input type="submit" value="Validate!"> 
</form> 
</div> 



<script> 
// avoids form submit 
jQuery.validator.setDefaults({ 
    debug: true, 
    success: "valid" 
}); 

$.validator.methods.email = function(value, element) { 
    return this.optional(element) || /^([\w-\.][email protected](?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/.test(value); 
} 

$("#myform").validate({ 
    rules: { 
    fname: { 
     required: true, 
     minlength: 1, 
     maxlength: 30, 
     lettersonly: true 
    }, 
    lname: { 
     required: true, 
     minlength: 1, 
     maxlength: 30, 
     lettersonly: true 
    }, 
    email: { 
     required: true, 
     email: true 
    }, 
    jtitle: { 
     required: true, 
     minlength: 1, 
     maxlength: 30 
    }, 
    orgname: { 
     required: true, 
     minlength: 1, 
     maxlength: 30 
    }, 
    desyourself: { 
     required: true, 
    } 
    } 
}); 
</script> 
+0

控制檯中的任何Java腳本錯誤? –

+0

_「爲什麼我的表單沒有提交?」_device''id'和'name'屬性在'input','select'元素上重複?請注意,'javascript'不是必須返回預期的結果;你可以使用'HTML5''required'屬性 – guest271314

回答

0

行之後阻止您的表單提交:

// avoids form submit 
jQuery.validator.setDefaults({ 
    debug: true, 
    success: "valid" 

});

0

調試(默認:false)類型:Boolean啓用調試模式。如果爲true,則不提交 表單,並在控制檯上顯示某些錯誤 (將檢查window.console屬性是否存在)。嘗試啓用 表單,而不是驗證停止提交。

https://jqueryvalidation.org/validate/