我想使用jQuery驗證插件,並顯示消息的問題。jQuery驗證 - 錯誤不顯示
該字段本身變成紅色,但消息無處可見。
我的HTML代碼:
<div class="dispTbl form-group">
<div class="dispCell">
<label>Send discount message within</label>
<div class="error" id="offerError"></div>
</div>
<div class="dispCell" style="vertical-align:middle;line-height:20px">
<div class="InputAddOn">
<button class="InputAddOn-item" data-action="decrease" data-field="offerDays"><i class="fa fa-arrow-circle-down"></i></button>
<input type="text" name="settings__discount_message_offer_days" id="offerDays" class="days" style="width: 46px" value="<?= $settings__discount_message_offer_days; ?>">
<button class="InputAddOn-item" data-action="increase" data-field="offerDays"><i class="fa fa-arrow-circle-up"></i></button>
</div>
</div>
我jQuery代碼:
var validator = $("#settingsForm").bind("invalid-form.validate", function() {
$("#errors").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
}).validate({
debug: true,
errorElement: "div",
errorContainer: $("#warning, #summary"),
errorPlacement: function(error, element) {
if(element.attr("class") == "days")
error.appendTo($('#offerError'));
},
success: function(label) {
label.text("ok!").addClass("success");
},
rules: {
settings__discount_message_offer_days: {
smaller: $('#discApplicableDays').val(),
number: true
}
},
messages: {
settings__discount_message_offer_days: {
number: 'Please enter a numeric value'
}
}
});
越小方法:
$.validator.addMethod('smaller',function(value, element, param) {
return value <= param;
}, 'These number of days cannot be bigger than the Discount applicable if paid within number of days');
方法得到的值是: 值:1個 PARAM :0
爲什麼錯誤的div保持爲空?
好的,出於某種原因,錯誤突然顯示,但現在我有一個不同的問題。它總是顯示'較小'的消息,而不是數字消息。如果我在字段中加入'a',我會從較小的方法中得到錯誤。這是爲什麼? –
你所發佈的代碼根本不起作用...... HTML甚至不包含'form'標籤,'submit'按鈕或'#errors'和'#discApplicableDays'元素。請包含足夠的代碼以正確重現問題。 – Sparky