我有两个下拉列表(开始时间和结束时间),我想使用 jquery validate 进行验证,确保结束时间大于开始时间,但我无法让它工作。无论我放什么,它总是显示错误消息。有人可以看看我的代码并告诉我我做错了什么吗?
<label for="start_time">Start Time:<font color="red">*</font></label>
<select name = 'start_time_hour'>
<option value = '10'>10</option>
<option value = '11'>11</option>
<option value = '12'>12</option>
</select>
<label for="end_time_hour">End Time:<font color="red">*</font></label>
<select name = 'end_time_hour'>
<option value = '10'>10</option>
<option value = '11'>11</option>
<option value = '12'>12</option>
</select>
javascript
$(document).ready(function(){
jQuery.validator.addMethod("le", function(value, element, param) {
return this.optional(element) || value <= $(param).val();
}, "less");
jQuery.validator.addMethod("ge", function(value, element, param) {
return this.optional(element) || value >= $(param).val();
}, "greater");
$("#add").validate({
event: "keyup",
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
},
rules:{
start_time_hour:{le: "#end_time_hour"},
end_time_hour:{ge: "#start_time_hour"},
},
messages:{
start_time_hour:{le: "Must be less than end time"},
end_time_hour:{ge: "Must be greater than start time"},
},
});
});