客户端验证未在asp.net mvc的3个工作隐藏场 [英] Client side validation not working for hidden field in asp.net mvc 3

查看:130
本文介绍了客户端验证未在asp.net mvc的3个工作隐藏场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏字段与它下面

I have got a hidden field with a validation for it as below

@Html.HiddenFor(m => m.Rating)
@Html.ValidationMessageFor(m => m.Rating)

评级酒店与范围是1-5适用范围验证属性。这是把一个表单内有一个提交按钮。

The Rating property has Range validator attribute applied with range being 1-5. This is put inside a form with a submit button.

然后我有以下jQuery的,设置在一些用户事件中的隐藏字段中的值(在某些恒星基本上用户点击率)

I have then got following jquery that sets the value in hidden field on some user event (Basically user clicks on some stars to rate)

$(".star").click(function(){
    $("#Rating").val(2);
});

现在,如果我提交表单,而不用于设置隐藏字段用户事件,验证工作。该错误信息是正确显示和它的作品的所有客户端。

Now if I submit the form without the user event that sets the hidden field, the validation works. The error messages is displayed properly and it works all client side.

现在,在这种情况下,如果我点击星星,调用以上的JavaScript一台隐藏字段,验证错误信息不会消失。我可以提交表单的隐藏变量有一定的有效值之后。但我期待的客户端验证应该工作。 (当隐变量已经设置了一些有效的值,验证错误应该消失)

Now, in this situation, if I click on stars, that invokes the above javascript a sets the hidden field, the validation error message would not go away. I can submit the form after the hidden variable has some valid value. But I'm expecting that the client side validation should work. (When the hidden variable has been set with some valid value, the validation error should go away)

起初我想,jQuery验证将在一些特殊事件进行调用,所以我尝试提高点击,变更,KEYUP,模糊和focusOut事件自己如下

Initially I thought, the jquery validation would be invoked on some special events so I tried raising click, change, keyup, blur and focusout events myself as below

$(".star").click(function(){
    $("#Rating").val(2);
    $("#Rating").change();
});

但是,这仍然没有工作。错误消息一经问世,不会消失的。

But this is still not working. The error messages once appeared, does not go away at all.

推荐答案

在code这设置隐藏字段的值,手动调用验证表单,像这样:

In the code which sets the hidden field's value, manually invoke validation for the form, like so:

$("form").validate().form();

这篇关于客户端验证未在asp.net mvc的3个工作隐藏场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆