[英]jQuery validator errorplacement
My jQuery has the following code: 我的jQuery具有以下代码:
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).addClass('errorImage').removeClass(validClass).removeClass('validImage');
if($(element.form).find("input[type=radio") == true) {
$(element.form).find("input[id=" + element.id + "]").addClass(errorClass).addClass('errorImageRadio');
}else{
$(element.form).find("input[id=" + element.id + "]").addClass(errorClass).addClass('errorImage');
}
},
What I am trying is, if the input type is radio or checkbox, it should basically use a different class, I am missing something here, not sure what is that. 我正在尝试的是,如果输入类型是单选或复选框,则基本上应该使用其他类,我在这里遗漏了一些东西,不确定是什么。
It is a part of jQuery validator plugin . 它是jQuery验证程序插件的一部分。
jQuery objects are always truthy, if you want to test whether the jQuery objects matches any DOM elements, use the .length
property. jQuery对象始终是真实的,如果要测试jQuery对象是否与任何DOM元素匹配,请使用
.length
属性。
You also have a missing square bracket in your .find()
query: 您的
.find()
查询中也缺少方括号:
Instead of 代替
if($(element.form).find("input[type=radio") == true)
Try: 尝试:
if($(element.form).find("input[type=radio]").length)
For what it's worth (quite a lot) you should use the ID selector to target elements by their ID, not the attribute selector. 对于有价值的东西(很多),应该使用ID选择器通过元素的ID而不是属性选择器来定位元素。 Since you already have the DOM element in a variable, you can just wrap that in a jQuery object:
由于您已经在变量中包含DOM元素,因此可以将其包装在jQuery对象中:
$(element.form).find("input[id=" + element.id + "]").addClas...
Becomes 成为
$(element).addClas...
Etc... 等等...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.