将类添加到select2元素 [英] Add class to select2 element

查看:97
本文介绍了将类添加到select2元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档太糟糕了或者我丢失了一些东西.我正在尝试将一个错误类添加到select2框中以进行表单验证.只是一个1像素的红色边框.

The documentation is either terrible or I'm missing something. I'm trying to add an error class to a select2 box for form validation. It's just a 1px red border.

我在文档中找到了containerCssClass方法,但不确定如何应用.

I found the containerCssClass method in the documentation, but I'm not sure how to apply it.

我没有运气就尝试过以下方法:

I have tried the following with no luck:

$("#myBox").select2().containerCssClass('error');

推荐答案

jQuery使用JSON对象进行初始化,因此我想这也将:\

jQuery uses JSON objects to initialize, so I guess this one will as well:\

$("#myBox").select2({ containerCssClass : "error" });

如果您要添加/删除类,可以在初始化后进行

If you want to add/remove a class you can do this after you initialized it

$($("#myBox").select2("container")).addClass("error");
$($("#myBox").select2("container")).removeClass("error");

上面的函数获取select2主容器的DOM节点,例如:$("#myBox").select2("container")

The above function gets the DOM node of the main container of select2 e.g.: $("#myBox").select2("container")

重要
您将需要使用container方法来获取容器,因为您不会直接编辑SELECT,但是select2将生成其他HTML来模拟可样式化的SELECT.

Important
You will need to use the container method to get the container since you won't edit the SELECT directly but select2 will generate other HTML to simulate a SELECT which is stylable.

这篇关于将类添加到select2元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆