对于mvc4中用于模型验证的隐藏模型元素,不显示qtip2

时间:2013-12-10 06:29:58

标签: jquery asp.net-mvc asp.net-mvc-4 qtip qtip2

我正在使用qtip2在我的MVC4应用程序中显示客户端验证。以下是代码

$(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 = ['top center', 'bottom center'];
                var flipIt = $(inputElem).parents('span.right').length > 0;

                // Hide the default validation error
                $(this).hide();

                // 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
                        //modal: {
                        //    on: true
                        //}                                
                    },
                    //hide: false,
                    hide: {
                        event: 'unfocus'//event: 'click mouseleave'
                    },
                    //style: { classes: 'ui-tooltip-red' }
                });
            });
        });

This is working fine until the element on which error message is to be displayed is visible.
I am using the kendo ui MVC helpers for controls like dropdownbox ,selectlist etc.
it hides the original element with its own.following the html generated (check element with id PhysicianType)

    <span class="k-widget k-dropdown k-header input-validation-error" style="margin: 0px 0px 0px 28px; width: 181px; font-family: 'Segoe UI';" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="PhysicianType_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="PhysicianType_option_selected">
    <span class="k-dropdown-wrap k-state-default" unselectable="on">
    <span class="k-input" unselectable="on">Select Type</span>
    <span class="k-select" unselectable="on">
    </span>
    <input id="PhysicianType" class="input-validation-error" type="text" style="margin: 0px 0px 0px 28px; width: 181px; font-family: 'Segoe UI'; display: none;" name="PhysicianType" data-role="dropdownlist">
    </span>

现在我想显示PhysicianType的错误消息,但是它不可见;不显示错误消息。我使用以下代码显示隐藏元素的工具提示

 $(function () {
        $.validator.setDefaults({ ignore: [] });
    });

上面的代码也显示隐藏元素的错误代码,但qtip的位置是屏幕的左上角;没有附加到元素本身。
如果隐藏元素,请帮助准确定位qtip2错误。

1 个答案:

答案 0 :(得分:0)

if (somecondition.value() == 0) { $('span:first', "#NegCdtDDl").qtip({ content: { text: "Please Select value for control" }, show: { ready: true }, position: { at: 'top center', my: 'bottom center', viewport: $(window) }, hide: false }); } else { $('span:first', "#NegCdtDDl").qtip('destroy'); }