【问题标题】:ASP.Net MVC: How to attach unobtrusive validation message to controlsASP.Net MVC:如何将不显眼的验证消息附加到控件
【发布时间】:2016-01-15 13:31:51
【问题描述】:

我不想显示验证消息,而是想将验证消息附加到控件的标题属性,我已经完成了,但是工具提示没有显示标题属性,并且验证失败时也没有出现红色边框。

这里我附上了小示例代码,我遵循它来将验证消息附加到控件属性。

$("span[class='field-validation-error']").each(function () {

$(this).addClass("hidden");//Add class hidden to hide  @@Html.ValidationMessageFor(model => model.xyz) if using bootstrap , else use css
            var inputID = $(this).attr("data-valmsg-for");//get the id of the input field for which this validation prompted
            var validationMessage = $(this).html();//Get validation message for input filed which is prompted          
            //$("#" + inputID).tooltip({ 'trigger': 'hover', 'title': validationMessage });//Trigger the tooltip now, if using bootstrap.

                      //******OR*******

            $("#" + inputID).attr("title",validationMessage);
              });           
        }
        return false;
    });

我的完整 jquery 和 mvc 代码在 dotnet fiddle 中,其链接是 https://dotnetfiddle.net/30YQEX

所以请有人检查代码并与我讨论我在那里做错了什么?

1) 为什么将鼠标悬停在导致验证失败的控件上时没有出现工具提示?

2) 为什么验证失败时没有出现红色边框?

谢谢

【问题讨论】:

    标签: jquery asp.net-mvc validation


    【解决方案1】:

    您的$("span[class='field-validation-error']").each ... 代码在页面打开时执行。相反,它应该在验证发生后执行。只有这样错误 css 类才会应用于控件。

    最好的方法是覆盖 jQuery Validate 默认事件:

    $.validator.setDefaults({
        highlight: function (element) {
            $(element).   //do your custom error displaying here
        },
        unhighlight: function (element) {
            //hide your custom error here
        },
        ignore: [],
    });
    

    【讨论】:

    • 你给我的代码是一个小sn-p,从snip中我没有想到这个概念在highlight: function 里面添加什么?你能把我的js代码添加到highlight: function
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2011-07-09
    • 2011-07-13
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多