Nếu có một lỗi xác nhận server-side, khi tải trang sẽ có một phần tử span với lớp 'trường xác nhận lỗi' vì vậy chúng tôi có thể chỉ đơn giản lặp trên tất cả các yếu tố với lớp đó , trích xuất nội dung hoặc thông báo lỗi và hiển thị nó trong một chú giải công cụ.
$(document).ready(function() {
// Run this function for all validation error messages
$('.field-validation-error').each(function() {
// Get the name of the element the error message is intended for
// Note: ASP.NET MVC replaces the '[', ']', and '.' characters with an
// underscore but the data-valmsg-for value will have the original characters
var inputElem = '#' + $(this).attr('data-valmsg-for').replace('.', '_').replace('[', '_').replace(']', '_');
var corners = ['left center', 'right center'];
var flipIt = $(inputElem).parents('span.right').length > 0;
// Hide the default validation error
$(this).addClass('Hidden');
// Show the validation error using qTip
$(inputElem).filter(':not(.valid)').qtip({
content: { text: $(this).text() } , // Set the content to be the error message
position: {
my: corners[flipIt ? 0 : 1],
at: corners[flipIt ? 1 : 0],
viewport: $(window)
},
show: { ready: true },
hide: false,
style: { classes: 'ui-tooltip-red' }
});
});
});
Đây là một blog post giải thích cách thực hiện điều này một cách chi tiết.
Nguồn
2011-08-11 14:59:54