MVC3 不显眼的验证将验证移至自定义元素
在服务器端我渲染一个隐藏字段,然后使用一个名为 Flexbox 的 jquery 小部件创建一个组合框,它在客户端创建一个输入元素,并在您在框中选择某些内容后将所选 ID(不是文本)复制到隐藏字段。
问题是,当验证出现问题时,验证代码会向隐藏字段添加一个类名,我希望将其添加到输入元素中,我可以以某种方式监听添加类名的时间,或者某种方式挂钩到事件并移动输入字段的类名。
这可行,但它很难看,想要一个更好的解决方案,
var oldClass = $hdn.attr('class');
setInterval(function () {
if (oldClass != $hdn.attr('class')) {
$input.removeClass(oldClass);
oldClass = $hdn.attr('class');
$input.addClass($hdn.attr('class'));
}
}, 200);
谢谢。
Serverside I render a hiddenfield, I then use a jquery widget called flexbox to create a combobox, it creates a input element client side and copies the selected ID (Not text) to the hidden field once you select something in the box.
The problem is that the validation code adds a classname to the hiddenfield when something is wrong with validation, I want it to be added to the input element, can I somehow listen to when the classname is added, or somehove hook into the event and move the classname to the inputfield.
This works but its ugly as hell, would like a better solution
var oldClass = $hdn.attr('class');
setInterval(function () {
if (oldClass != $hdn.attr('class')) {
$input.removeClass(oldClass);
oldClass = $hdn.attr('class');
$input.addClass($hdn.attr('class'));
}
}, 200);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在验证隐藏元素的地方,我添加了一个自定义属性
data-val-visibleid
。然后,在jquery.validate.js
中,我修改了highlight
和unhighlight
函数,在两个函数的末尾添加了以下内容:不喜欢干预 jquery.validate.js,但它通常是实现此类自定义的最简单方法。
更新
我做了一些研究,发现 jquery.validate 有一个漂亮的
setDefault
方法,您可以在其中覆盖默认函数,例如highlight() 和 unhighlight。加载其他脚本后,将以下内容添加到您的页面:这将覆盖默认函数,而不更改底层脚本。
Where I have a hidden element being validated, I add a custom attribute,
data-val-visibleid
. Then, injquery.validate.js
, I modify thehighlight
andunhighlight
functions by adding the following at the end of both functions:Some people do not like to meddle in jquery.validate.js, but it is usually the easiest method to achieve customizations like this.
UPDATE
I did some research, and discovered that jquery.validate has a nifty
setDefault
method, where you can override the default functions, such as highlight() and unhighlight. Add the following to your page after the other scripts have been loaded:This will override the default functions, without changing the underlying script.
感谢 Counsellorben,我找到了一个很好的解决方案,不过我的方式略有不同。
首先,我重写主对象构造函数中的默认方法,该构造函数是在 document.ready 中构造的。然而 document.ready 为时已晚,当从 form.valid() 进行触发验证时,您的方法不会触发,但是在提交时会触发(非常奇怪)此代码既适用于提交,也适用于从脚本触发
Thanks to Counsellorben i found a good solution, I did it in a slightly different way though.
First i override the default methods in my master object contructor which is is constructed at document.ready. document.ready is however too late and your methods will not trigger when doing a triggering validation from form.valid() it will however trigg when doing a submit (very strange) this code works both for submit and triggered from script
我发现这两个答案都非常有帮助,只是想为使用 1.9.0 版验证插件的任何人添加您需要覆盖忽略隐藏字段的默认行为,如另一篇文章中详细介绍的: jQuery Validate - 启用隐藏字段验证
I found both these answers to be very helpful and just wanted to add for anyone using version 1.9.0 of the Validation plugin that you will need to override the default behavior that ignores hidden fields as detailed in this other post: jQuery Validate - Enable validation for hidden fields