简体   繁体   English

无法使用select2读取jQuery验证上未定义的nodeName属性

[英]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应用于selecttextarea ,某些inputcontenteditable元素...别无其他。

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.

相关问题 select2:无法读取未定义的属性“id” - select2: cannot read property 'id' of undefined TypeError:无法读取未定义的属性“ nodeName” - TypeError: Cannot read property 'nodeName' of undefined 无法读取SVG文件中未定义的属性“ nodeName” - Cannot read property 'nodeName' of undefined in SVG file Select2 Uncaught TypeError:无法读取未定义的属性“调用” - Select2 Uncaught TypeError: Cannot read property 'call' of undefined 在Select2中出现无法读取未定义的属性&#39;createTextRange&#39; - In Select2 appears Cannot read property 'createTextRange' of undefined select2 ajax:未捕获的TypeError:无法读取未定义的属性“上下文” - select2 ajax : Uncaught TypeError: Cannot read property 'context' of undefined Trumboqyg 所见即所得编辑器不适用于 Jquery.Validate 插件,“Uncaught TypeError: Cannot read property 'nodeName' of undefined” - Trumboqyg WYSIWYG editor does not work with Jquery.Validate plugin, "Uncaught TypeError: Cannot read property 'nodeName' of undefined" bs-tooltip TypeError:无法读取未定义的属性“nodeName” - bs-tooltip TypeError: Cannot read property 'nodeName' of undefined 错误:“无法读取未定义的属性 'nodename'” 转换 html 表时 - Error : “cannot read property 'nodename' of undefined” While converting html Table jQuery-jquery .append()产生“无法读取null的属性&#39;nodeName&#39;” - JQuery - jquery .append() produces “Cannot read property 'nodeName' of null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM