我有一個非常複雜的表單的驗證方法,並且由我之前的前端開發人員編寫。有一個主要問題有兩個子問題,如果主要問題被切換,至少在子問題上必須切換。任何被切換的子問題都必須有評論。我必須對其進行修改,但首先要了解它是如何工作的。我希望有人可以回答以下問題:瞭解複雜的jQuery驗證規則
對於添加了addMethod的函數,這是一個自定義規則嗎?這就是爲什麼規則中的功能列出並設置爲「真」?
什麼是每個規則的defaultInvlid函數?我看到它返回true/false。返回true的效果是什麼?
爲什麼函數包含在消息中?這是否意味着如果函數返回true,顯示該消息?
$("#form1").validate({
errorLabelContainer: $("#form-error"),
rules: {
//sub fields question 1
'report_question_ID_product_received_as_ordered_SUB_missing_parts_comment': {
required: '#report_question_ID_product_received_as_ordered_SUB_missing_parts_not_acceptable:checked',
report1subfields: true,
defaultInvalid: {
depends: function (element) {
return $('#report_question_ID_product_received_as_ordered_SUB_missing_parts_not_acceptable').is(':checked') ? true : false;
}
}
},
'report_question_ID_product_received_as_ordered_SUB_other_issues_comment': {
required: '#report_question_ID_product_received_as_ordered_SUB_other_issues_not_acceptable:checked',
report1subfields: true,
defaultInvalid: {
depends: function (element) {
return $('#report_question_ID_product_received_as_ordered_SUB_other_issues_not_acceptable').is(':checked') ? true : false;
}
}
}
},
focusInvalid: false,
messages: {
'report_question_ID_product_received_as_ordered_SUB_missing_parts_comment': {
defaultInvalid: "Are there missing parts?",
report1subfields: "Did You Receive The Product As Ordered? > At least one subquestion is mandatory."
},
'report_question_ID_product_received_as_ordered_SUB_other_issues_comment': {
defaultInvalid: "Are there other issues with your order?",
report1subfields: "Did You Receive The Product As Ordered? > At least one subquestion is mandatory."
}
},
submitHandler: function (form) {
//SUBMIT
},
invalidHandler: function (event, validator) {
showFormError(validator);
}
}).settings.ignore = [];
HTML
<div id="question-form-wrapper">
<div id="form-error" style="display: none"></div>
<div class="questions-form" id="report-questions-form">
<div class="question question-textarea has-subquestions" runat="server" id="div0001_00" style="display:none;">
<div class="question-intro clearfix">
<h2 id="QuestionText0001_00" runat="server">Did You Receive The Product As Ordered?</h2>
<div class="no-yes answer-acceptable">
<div class="no"><label class="label-1" for="report_question_ID_product_received_as_ordered_not_acceptable">Not Acceptable</label></div>
<a href="#" class="toggle" runat="server" id="report_question_ID_product_received_as_ordered_link"></a>
<div class="yes"><label class="label-2" for="report_question_ID_product_received_as_ordered_acceptable">Acceptable</label></div>
<label class="universal-label"></label>
<input type="radio" id="report_question_ID_product_received_as_ordered_not_acceptable" name="report-question-ID-product-received-as-ordered" value="0" runat="server">
<input type="radio" id="report_question_ID_product_received_as_ordered_acceptable" name="report-question-ID-product-received-as-ordered" value="1" checked="true" runat="server">
</div>
</div>
<div class="question-content">
<div class="question-content-inner clearfix">
<div class="sub-questions-list">
<div class="sub-question sub-question-textarea" runat="server" id="div0001_01" style="display:none;">
<div class="sub-question-intro clearfix">
<h3 id="QuestionText0001_01" runat="server">Are there missing parts?</h3>
<div class="yes-no answer-acceptable">
<div class="yes"><label class="label-1" for="report_question_ID_product_received_as_ordered_SUB_missing_parts_not_acceptable">Yes</label></div>
<a href="#" class="toggle" runat="server" id="report_question_ID_product_received_as_ordered_SUB_missing_parts_link"></a>
<div class="no"><label class="label-2" for="report_question_ID_product_received_as_ordered_SUB_missing_parts_acceptable">No</label></div>
<label class="universal-label"></label>
<input type="radio" id="report_question_ID_product_received_as_ordered_SUB_missing_parts_not_acceptable" name="report-question-ID-product-received-as-ordered-SUB-missing-parts" value="1" runat="server">
<input type="radio" id="report_question_ID_product_received_as_ordered_SUB_missing_parts_acceptable" name="report-question-ID-product-received-as-ordered-SUB-missing-parts" value="0" checked="true" runat="server">
</div>
</div>
<div class="sub-question-content">
<div class="sub-question-content-inner clearfix">
<div class="rounded-corners">
<div class="rounded-corners-inner clearfix">
<div class="comment-wrapper">
<textarea class="comment-for-question" cols="10" rows="20" name="report_question_ID_product_received_as_ordered_SUB_missing_parts_comment" id="report_question_ID_product_received_as_ordered_SUB_missing_parts_comment" runat="server">Please provide comments. Up to 5 photos can be attached.</textarea>
</div>
<div class="upload-photos clearfix" runat="server">
<div class="upload-photos-add" id="Q0001_01" runat="server">
<asp:AjaxFileUpload EnableViewState="false" ID="AjaxFileUpload1" ContextKeys="0001.01" runat="server" AllowedFileTypes="jpg,jpeg,png" OnUploadComplete="AjaxFileUpload_UploadComplete" OnClientUploadComplete="onClientUploadComplete" OnClientUploadCompleteAll="onClientUploadCompleteAll" OnClientUploadStart="onClientUploadStart" OnClientUploadError="onClientUploadError"></asp:AjaxFileUpload>
</div>
<div class="upload-photos-list">
<div class="upload-photos-list-inner clearfix" runat="server" id="divUploadListDynamic0001_01">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sub-question sub-question-textarea" runat="server" id="div0001_02" style="display:none;">
<div class="sub-question-intro clearfix">
<h3 id="QuestionText0001_02" runat="server">Are there other issues with your order?</h3>
<div class="yes-no answer-acceptable">
<div class="yes"><label class="label-1" for="report_question_ID_product_received_as_ordered_SUB_other_issues_not_acceptable">Yes</label></div>
<a href="#" class="toggle" runat="server" id="report_question_ID_product_received_as_ordered_SUB_other_issues_link"></a>
<div class="no"><label class="label-2" for="report_question_ID_product_received_as_ordered_SUB_other_issues_acceptable">No</label></div>
<label class="universal-label"></label>
<input type="radio" id="report_question_ID_product_received_as_ordered_SUB_other_issues_not_acceptable" name="report-question-ID-product-received-as-ordered-SUB-other-issues" value="1" runat="server">
<input type="radio" id="report_question_ID_product_received_as_ordered_SUB_other_issues_acceptable" name="report-question-ID-product-received-as-ordered-SUB-other-issues" value="0" checked="true" runat="server">
</div>
</div>
<div class="sub-question-content">
<div class="sub-question-content-inner clearfix">
<div class="rounded-corners">
<div class="rounded-corners-inner clearfix">
<div class="comment-wrapper">
<textarea class="comment-for-question" cols="10" rows="20" name="report_question_ID_product_received_as_ordered_SUB_other_issues_comment" id="report_question_ID_product_received_as_ordered_SUB_other_issues_comment" runat="server">Please provide comments. Up to 5 photos can be attached.</textarea>
</div>
<div class="upload-photos clearfix" runat="server">
<div class="upload-photos-add" id="Q0001_02" runat="server">
<asp:AjaxFileUpload EnableViewState="false" ID="AjaxFileUpload2" ContextKeys="0001.02" runat="server" AllowedFileTypes="jpg,jpeg,png" OnUploadComplete="AjaxFileUpload_UploadComplete" OnClientUploadComplete="onClientUploadComplete" OnClientUploadCompleteAll="onClientUploadCompleteAll" OnClientUploadStart="onClientUploadStart" OnClientUploadError="onClientUploadError"></asp:AjaxFileUpload>
</div>
<div class="upload-photos-list">
<div class="upload-photos-list-inner clearfix" runat="server" id="divUploadListDynamic0001_02">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="actions">
<input type="image" src="assets/images/css/button-next-red.png" alt="Next" runat="server" id="btnNext">
</div>
</div><!-- #report-questions-form -->
</div><!-- #question-form-wrapper -->