2015-09-28 22 views
1

在我的情況下,我有各種元素,如輸入,選擇,複選框,收音機。有什麼方法可以找出表單是否有效並啓用提交按鈕。目前我正在做的是調用.valid()方法來查找表單的有效性,只要單擊我想要避免的任何元素,就會觸發元素的驗證並顯示錯誤消息。jQuery驗證插件,啓用提交按鈕時,表單有效,沒有熱切驗證

+3

能否請您提供撥弄它會很容易解決你的問題 – Mahadevan

+0

也許你可以看看jQuery的表單驗證插件? http://jqueryvalidation.org/ – Enermis

+0

這是,http://jsfiddle.net/maheshambiga/b1dL20e0/ –

回答

1

您是否在尋找這樣的解決方案:

$(document).ready(function(){ 
 
    
 
    $("input.submit").prop("disabled", true); 
 
    
 
    $('form').validate({ 
 
\t showErrors: function(errorMap, errorList) { 
 

 
\t \t var formSelector = '#' + this.currentForm.id; 
 
\t \t var formObject = $(formSelector); 
 
\t \t var validateObject = formObject.validate(); 
 
\t \t var numberOfInvalids = validateObject.numberOfInvalids(); 
 

 
\t \t if(numberOfInvalids == 0) 
 
\t \t $("input.submit").prop("disabled", false); 
 
\t \t else 
 
\t \t $("input.submit").prop("disabled", true); 
 
     
 
    } 
 
}); 
 
    
 
$("#commentForm").valid(); 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script> 
 
<form class="cmxform" id="commentForm" method="get" action=""> 
 
\t <fieldset> 
 
\t <legend>Please provide your name, email address (won't be published) and a comment</legend> 
 
\t <p> 
 
\t <label for="cname">Name (required, at least 2 characters)</label> 
 
\t <input id="cname" name="name" minlength="2" type="text" required> 
 
\t </p> 
 
\t <p> 
 
\t <label for="cemail">E-Mail (required)</label> 
 
\t <input id="cemail" type="email" name="email" required> 
 
\t </p> 
 
\t <p> 
 
\t <label for="curl">URL (optional)</label> 
 
\t <input id="curl" type="url" name="url"> 
 
\t </p> 
 
\t <p> 
 
\t <label for="ccomment">Your comment (required)</label> 
 
\t <textarea id="ccomment" name="comment" required></textarea> 
 
\t </p> 
 
\t <p> 
 
\t <input class="submit" type="submit" value="Submit" > 
 
\t </p> 
 
\t </fieldset> 
 
</form>

相關問題