无法使用select2读取jQuery验证上未定义的nodeName属性 [英] Cannot read property of nodeName undefined on jQuery validation with select2
问题描述
我正在尝试在我的select2下拉列表中添加jQuery验证.但是,它一直导致javascript异常"nodeName undefined",我不确定出了什么问题.
I am trying to add jQuery validation on my select2 drop-down. However, it keeps causing the javascript exception of "nodeName undefined", I am not sure what went wrong.
$(".field-validation-required").each(function() {
// output SELECT, if current html class is a select2 dropdown.
console.log(this.nodeName);
$(this).rules('add', {
required: true,
messages: {
required: "This field is required"
}
});
});
我的select2下拉列表包含类'.field-validation-required',我正在尝试对其应用一些jQuery验证.但是,似乎$(this)总是导致无法读取nodeName未定义的属性"错误.
My select2 dropdown contains the class '.field-validation-required' and I am trying to apply some jQuery validation on it. But it seems $(this) always causes that "Cannot read property of nodeName undefined" error.
推荐答案
我的select2下拉列表包含类
.field-validation-required
,我正在尝试对其进行一些jQuery验证.但是,似乎$(this)
总是导致无法读取未定义的nodeName属性"
错误.
My select2 dropdown contains the class
.field-validation-required
and I am trying to apply some jQuery validation on it. But it seems$(this)
always causes that"Cannot read property of nodeName undefined"
error.
向我们展示相关的HTML标记;否则,我无法复制您的错误.
Show us the relevant HTML markup; otherwise, I cannot duplicate your error.
您原来的 select
是否包含唯一的名称
?这是由插件强制执行的,缺少或重复的 name
可能是错误消息的根本原因.
Does your original select
contain a unique name
? This is mandated by the plugin and a missing or duplicate name
may be the root cause of your error message.
您的 .field-validation-required
类是原始的 select
元素还是动态创建的Select2元素?
Is your .field-validation-required
class on the original select
element or on your dynamically created Select2 elements?
如果它位于 select
元素上,则它应该可以工作.如果它在Select2元素上,那么它将无法正常工作.
If it's on the select
element, then it should work. If it's on the Select2 element, then it will not work.
您只能将jQuery Validate应用于 select
, textarea
,某些 input
和 contenteditable
元素.什么都没有.
You can only apply jQuery Validate to select
, textarea
, certain input
, and contenteditable
elements... nothing else.
定位原始隐藏的 select
元素,而不是Select2元素.然后,由于该插件将忽略所有隐藏的元素,因此您需要将 ignore
选项设置为某些jQuery选择器,该选择器不会忽略您的隐藏的 select
元素.例如, ignore:[]
会告诉插件什么也不忽略,并验证所有隐藏的元素.
Target the original hidden select
element instead of the Select2 element. Then since the plugin will ignore all hidden elements, you'll need to set the ignore
option to some jQuery selector that does not ignore your hidden select
element. For example, ignore: []
will tell the plugin to ignore nothing and validate all hidden elements.
或者,您可以按类别或HTML5属性将 required
规则应用于原始的 select
元素.您仍然需要如上所述设置 ignore
选项.
Alternatively, you can apply the required
rule to your original select
element by class or HTML5 attribute. You'll still need to set the ignore
option as described above.
<select name="myselect" class="required" ....
或
<select name="myselect" required="required" ....
这篇关于无法使用select2读取jQuery验证上未定义的nodeName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!