我目前有一個關於2 .jsp頁面和一些jQuery驗證的驗證問題。對mvc選擇標記的錯誤驗證
問題簡而言之:我的頁面上有一組select元素。沒有一個字段是空的。但是,當我想提交表單時,這些字段被標記爲無效。 jQuery代碼位於文件searchbox.jsp中,實際的表單位於文件searchform.jsp中。 Searchform正在包含在搜索框文件中。
這裏是驗證代碼片段:
jQuery("#car-search").validate({
// onkeyup: false,
onfocusout: false,
focusInvalid: false,
focusCleanup: true,
rules : {
"pickupLocationName" : {
required : true,
fullLocation : true,
minlength : 3
},
"pickupDate" : {
required : true,
minlength : 1
},
"pickupTimeHours": {
required: true
},
"returnLocationName": {
required: true,
fullLocation : true,
minlength: 3
},
"returnDate" : {
required : true,
minlength : 1
},
"returnTimeHours": {
required: true
},
"driverFirstname": {
required: true,
minlength: 2
},
"driverLastname": {
required: true,
minlength: 2
},
"driverBirthdate": {
required: true,
minlength: 10,
maxlength: 10,
realDate: true
}
},
messages : {
"pickupLocationName" : "<spring:message code="car_search.pickup_location.required" />",
"returnLocationName" : "<spring:message code="car_search.return_location.required" />",
"pickupDate" : "<spring:message code="car_search.pickup_date.required" />",
"returnDate" : "<spring:message code="car_search.return_date.required" />",
"pickupTimeHours": "<spring:message code="car_search.pickup_time.required" />",
"returnTimeHours": "<spring:message code="car_search.return_time.required" />",
"driverFirstname": "<spring:message code="car_search.driver_firstname.required"/>",
"driverLastname": "<spring:message code="car_search.driver_lastname.required"/>",
"driverBirthdate": "<spring:message code="car_search.driver_birthdate.required"/>"
},
showErrors : function(errorMap, errorList) {
if (!validationMessageIsShown) {
jQuery(".invalidFormMessage #invalidFormItems").text("");
this.defaultShowErrors();
if(this.numberOfInvalids()>0){
jQuery(".invalidFormMessage").jqmShow();
}
validationMessageIsShown = true;
}
},
invalidHandler : function (form, validator) {
validationMessageIsShown = false;
},
submitHandler: function(form) {
showLoading();
form.submit();
},
errorPlacement : function(error, element) {
error.appendTo("#invalidFormItems");
}
});
而且這個片段的形式:
<form:form commandName="carSearchForm" method="post" action="${actionUrl}" id="car-search" cssClass="searchForm">
<fieldset>
...
<div class="blueDottedBorder"></div>
</div>
<div class="usaStep step3">
<h4><spring:message code="car_search.driver_section"/> <span class="normal"><spring:message code="car_search.driver_section_info"/></span></h4>
<div class="clr">
<form:label path="driverFirstname" cssClass="big"><spring:message code="car_search.driver_firstname"/></form:label>
<c:choose>
<c:when test="${empty adultFirstNames}">
<form:input path="driverFirstname" id="driverFirstname"/>
</c:when>
<c:otherwise>
<form:select path="driverFirstname" id="driverFirstname" >
<form:options items="${adultFirstNames}" />
</form:select>
</c:otherwise>
</c:choose>
</div>
<div class="clr">
<form:label path="driverLastname" cssClass="big"><spring:message code="car_search.driver_lastname"/></form:label>
<c:choose>
<c:when test="${empty adultLastNames}">
<form:input path="driverLastname" id="driverLastname" />
</c:when>
<c:otherwise>
<form:select path="driverLastname" id="driverLastname" >
<form:options items="${adultLastNames}" />
</form:select>
</c:otherwise>
</c:choose>
</div>
<div class="clr">
<form:label path="driverBirthdate" cssClass="big"><spring:message code="car_search.driver_birthdate"/></form:label>
<c:choose>
<c:when test="${empty adultBirthdays}">
<form:input path="driverBirthdate" size="10" maxlength="10" cssClass="birthdate" id="driverBirthdate" />
</c:when>
<c:otherwise>
<form:select path="driverBirthdate" items="${adultBirthdays}" id="driverBirthdate" >
<form:options items="${adultBirthdays}" />
</form:select>
</c:otherwise>
</c:choose>
</div>
<div class="info-msg" style="padding-left: 30px; margin-top: 30px;" id="extraPointCost"><spring:message code="car_search.warning_surcharge" /> <a href="<url:car-rental_drop-off-cost />" target="_blank" class="more"><spring:message code="car_search.warning_surcharge.link_label" /></a></div>
</div>
<div style="margin-top: 30px;"><a href="<url:car-rental_conditions />" target="_blank" class="more"><spring:message code="car_search.rental_conditions.link_label" /></a></div>
</div>
<input type="submit" class="submit" value="<spring:message code="car_search.submit"/> »"/>
</fieldset>
</form:form>
我已刪除了不相關的領域出形式。該問題僅適用於driverFirstname,driverLastname和driveBirthdate字段。
在沒有值的情況下(所以當插入的列表在選項標籤中爲空時),一切正常。驗證在當前常規輸入字段的字段上正常工作。
當列表不爲空時,所以當至少有一個或多個選項可供選擇。正如您在表單片段中看到的那樣,實際上有選項時,輸入字段將變爲選擇字段。那麼表單不會通過驗證,即使字段不是空的,並且值的大小大於2.
這真的令我感到困惑。我一直在搜索與選擇領域的已知問題,我在這個論壇上看到了相當多的帖子,但沒有一個建議的解決方案工作或將工作(我必須承認,我沒有嘗試過所有,因爲一些似乎真的不適合什麼我想要)。
有什麼想法?
在此先感謝。
顯示了在瀏覽器中呈現最終的HTML代碼。 – Sparky 2013-03-07 17:19:55