[英]Cannot read property of nodeName undefined on jQuery validation with select2
I am trying to add jQuery validation on my select2 drop-down. 我试图在我的select2下拉列表中添加jQuery验证。 However, it keeps causing the javascript exception of "nodeName undefined", I am not sure what went wrong. 但是,它一直导致javascript异常“ nodeName undefined”,我不确定出了什么问题。
$(".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"
}
});
});
My select2 dropdown contains the class '.field-validation-required' and I am trying to apply some jQuery validation on it. 我的select2下拉列表包含类'.field-validation-required',并且我正在尝试对其应用一些jQuery验证。 But it seems $(this) always causes that "Cannot read property of nodeName undefined" error. 但是,似乎$(this)总是导致“无法读取nodeName未定义的属性”错误。
My select2 dropdown contains the class
.field-validation-required
and I am trying to apply some jQuery validation on it. 我的select2下拉列表包含.field-validation-required
类.field-validation-required
并且我正在尝试对其应用一些jQuery验证。 But it seems$(this)
always causes that"Cannot read property of nodeName undefined"
error. 但是,似乎$(this)
总是导致"Cannot read property of nodeName undefined"
错误。
Show us the relevant HTML markup; 向我们展示相关的HTML标记; otherwise, I cannot duplicate your error. 否则,我无法复制您的错误。
Does your original select
contain a unique name
? 您的原始select
是否包含唯一name
? This is mandated by the plugin and a missing or duplicate name
may be the root cause of your error message. 这是插件要求的, name
丢失或重复可能是错误消息的根本原因。
Is your .field-validation-required
class on the original select
element or on your dynamically created Select2 elements? 您的.field-validation-required
类是在原始select
元素上还是在动态创建的Select2元素上?
If it's on the select
element, then it should work. 如果它在select
元素上,那么它应该可以工作。 If it's on the Select2 element, then it will not work. 如果它在Select2元素上,那么它将无法工作。
You can only apply jQuery Validate to select
, textarea
, certain input
, and contenteditable
elements... nothing else. 您只能将jQuery Validate应用于select
, textarea
,某些input
和contenteditable
元素...别无其他。
Target the original hidden select
element instead of the Select2 element. 定位到原始的隐藏select
元素,而不是Select2元素。 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. 然后,由于该插件将忽略所有隐藏的元素,因此您需要将ignore
选项设置为某些jQuery选择器,该选择器不会忽略您的隐藏的select
元素。 For example, ignore: []
will tell the plugin to ignore nothing and validate all hidden elements. 例如, ignore: []
将告诉插件什么也不忽略,并验证所有隐藏的元素。
Alternatively, you can apply the required
rule to your original select
element by class or HTML5 attribute. 或者,您可以按类别或HTML5属性将required
规则应用于原始的select
元素。 You'll still need to set the ignore
option as described above. 您仍然需要如上所述设置“ ignore
选项。
<select name="myselect" class="required" ....
or 要么
<select name="myselect" required="required" ....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.