简体   繁体   English

使用jQuery.validate进行Select2设置未定义错误

[英]Select2 settings undefined error with jquery.validate

I am using Select2 as my dropdown. 我使用Select2作为下拉菜单。 I am getting the following error even if I click inside dropdown: 即使单击内部下拉菜单,我也会收到以下错误:

在此处输入图片说明 This error occurs only when I use select2 inside my form, outside the form it is working perfectly. 仅当我在窗体外部使用select2时,才会发生此错误,而在窗体外部它运行正常。

Here is my code: 这是我的代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

<script type="text/javascript">
    $("#ddlUsers").select2();
</script>
<form class="form-horizontal">
    <div class="form-group">
          <label class="col-sm-2 control-label">Add User</label>
          <div class="col-sm-10">
               <select id="ddlUsers" class="form-control" multiple="multiple">
                   <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
           </div>
     </div>
     <div class="form-group">
           <div class="col-sm-offset-2 col-sm-10">
               <button type="button" onclick="onAddclick();return false;" class="btn btn-primary">Add User</button>
            </div>
      </div>
</form>

In the Select2 examples they Use classes to define the selected options have you tried this approach as the jQuery may be using the ID attribute for some thing else. 在Select2示例中,您是否尝试过这种方法,所以它们使用类来定义选定的选项,因为jQuery可能将ID属性用于其他用途。

Also you label usually has the same ID as the input field it is for. 另外,标签通常与输入字段具有相同的ID。

The "Inspect" image shows that the JavaScript/jQuery code generated six (6) errors. “检查”图像显示JavaScript / jQuery代码生成了六(6)个错误。

在此处输入图片说明

At least two (2) of the errors were due to a TypeError about "settings". 至少有两(2)个错误归因于有关“设置”的TypeError。 Since this code was not included in the original post, it is difficult to diagnose your problems with the information provided. 由于此代码未包含在原始帖子中,因此很难通过提供的信息来诊断您的问题。

Review the error diagnostic information shown by "Inspect" and make the code changes to avoid these errors. 查看“检查”显示的错误诊断信息,并进行代码更改以避免这些错误。

I'm not 100% if it's the case here, but this error can occur when the form contains dynamically added elements, that were not initialised by jQuery.validate .validator (validator property/object was not set). 如果不是这种情况,我不是100%,但是当表单包含动态添加的元素(不是由jQuery.validate .validator初始化(未设置Validator属性/对象))时,会发生此错误。 Then, as part of validation process, new element's .validator object's .settings property is accessed, but it's not there. 然后,作为验证过程的一部分,访问新元素的.validator对象的.settings属性,但不存在。 I would suggest trying to run .validate() method on the form, immediately and after addition of any new element to the DOM. 我建议立即在向DOM添加任何新元素之后立即尝试在表单上运行.validate()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM