我制作了一个简单的脚本来验证我的表单元素,使用 jQuery Validation 脚本和 tooltipster 来显示错误消息。
脚本工作正常,但不适用于选择菜单
$(document).ready(function(){
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "Value must not equal arg.");
$('#eway input[type="text"]').tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right'
});
$('.inputfield select').tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right'
});
(function($,W,D)
{
var JQUERY4U = {};
JQUERY4U.UTIL =
{
setupFormValidation: function()
{
//form validation rules
$("#eway").validate({
errorPlacement: function (error, element) {
$(element).tooltipster('update', $(error).text());
$(element).tooltipster('show');
},
rules: {
txtFirstName: "required",
txtLastName: "required",
txtEmail: {
required: true,
email: true
},
txtMobileNumber: "required",
Type: {
valueNotEquals: "Default"
}
},
messages: {
txtFirstName: "Please enter your firstname",
txtLastName: "Please enter your lastname",
txtEmail: "Please enter a valid email address",
txtMobileNumber: "please enter a valid mobile number",
Type: "Please select Donation Type"
},
submitHandler: function(form) {
form.submit();
}
});
}
}
//when the dom has loaded setup form validation rules
$(D).ready(function($) {
JQUERY4U.UTIL.setupFormValidation();
});
})(jQuery, window, document);
$('#eway').submit(function(e) {
e.preventDefault();
});
});
我不知道是什么问题。
可能是因为 tooltipster 功能显示选择菜单的错误消息我错过了什么?
$('.inputfield select').tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right'
});
还是使用我添加的验证方法来验证选择菜单?
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "Value must not equal arg.");