将jQuery验证附加到替换元素 [英] attaching jquery validation to replacement element

查看:79
本文介绍了将jQuery验证附加到替换元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带下拉列表的aspnet mvc表单,正在用一个jQuery组合框替换.

I have an aspnet mvc form with a dropdown list in, that I am replacing with a jquery combobox.

原始下拉列表具有针对它的客户端验证集-使用不显眼的lib以及标准的jquery validate-,我希望将此验证附加到组合框.

the original dropdownlist has a client validation set against it - using the unobtrusive lib along with the standard jquery validate - , and I would like this validation attached to the combobox instead.

作为组合框生成的一部分,该名称已从放置器中删除,而是设置在隐藏字段中. 我面临的问题是,当无效时(在这种情况下为空),组合框(隐藏字段)的验证消息将不显示.

as part of the generation of the combobox, the name is removed from the dropper and instead set on a hidden field. the problem I am facing is that the validation message is then not showing for the combobox (hidden field), when it is invalid (empty in this case).

是否有任何方法可以连接隐藏字段以进行验证(如下拉列表所示)? 我尝试了几件事:

is there any way of hooking up the hidden field to validate as the dropdown list would have? I have tried a couple of things:

  1. 将隐藏字段的名称设置为下拉名称-将data-val和data-val-required属性复制过来,但是我对此并不走运.我意识到,随着动态添加隐藏字段,验证器将不会选择它,但是当验证通过名称(afaik)进行选择时,验证器可能会起作用.我试图重新解析该表单,以便它选择隐藏的字段,但是由于原始规则仍然存在,所以似乎不起作用.
  2. 我还尝试过不使用隐藏字段,而是将验证保留在原始的放置器上-当设置组合框中的项目时,该设置也会在放置器中设置值.这也行不通-我想是因为下拉列表是隐藏的,因为如果我不显示它,则表明验证工作正常.

我正在使用引导jquery组合框 ,我对其进行了略微修改,以与选项组一起使用

I am using bootstrap jquery combobox, which I have amended slightly to work with option groups

任何人都可以提供一些帮助吗?

can anyone offer some assistance please?

谢谢

推荐答案

您正在使用的插件会隐藏输入,并将其替换为自己的html.默认情况下,jQuery验证不会验证隐藏的表单控件,但是您可以通过修改验证器来覆盖此行为

The plugin you're using hides the input and replaces it with its own html. By default, jQuery validation does not validate hidden form controls but you can override this behavior by modifying the validator

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

请注意,如果您还有其他不想验证的隐藏元素,则可以为该元素指定一个类名

Note that if you have other hidden elements that you don't want validated, then you could give the element a class name

$.validator.setDefaults({ 
    ignore: ":hidden:not('.combobox')"
});

注意:请确保此脚本不在$(document.ready)函数之外.

Note: Make sure that this script is outside the $(document.ready) function.

这篇关于将jQuery验证附加到替换元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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