我正在尝试从输入的 html5 数据属性中读取一个集合,该集合被转换为 select2 以创建标签。
当我有一个输入时,这是有效的:
$(".tags").select2(
width: '220px'
tags: $(".tags").data('collection')
)
但我想使用元素本身的数据更安全,我尝试了以下方法:
$(".tags").select2(
width: '220px'
tags: $(this).data('collection')
)
但它失败并出现错误:
Uncaught query function not defined for Select2 investigador_aplicaciones
您知道是否可以将元素本身与 $(this) 等特定选择器一起使用?
最佳答案
您可以这样做:
$(".tags").each(function(){
var $this = $(this);
$this.select2({
width: '220px',
tags: $this.data('collection')
});
});
因为在您的调用过程中,this
并不代表选择器中的元素。
关于jquery - 在 select2 中使用 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17436253/