在select2中使用$(this) [英] Using $(this) in select2

查看:189
本文介绍了在select2中使用$(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从输入的html5数据属性中读取一个集合,该集合将转换为select2以创建标签.

I'm trying to read a collection from the html5 data-attribute of the input that is converted to select2 to create tags.

当我有一个输入时,这是可行的:

This is working when I have one input:

$(".tags").select2(
  width: '220px'
  tags: $(".tags").data('collection')
)

但是我想使用元素本身的数据更安全,我尝试过:

But I will like to do it more safe using the data of the element itself, I tried this:

$(".tags").select2(
  width: '220px'
  tags: $(this).data('collection')
)

但失败并显示以下错误:

But it fails with the error:

Uncaught query function not defined for Select2 investigador_aplicaciones

您知道是否可以将元素本身与特定的选择器(例如$(this))一起使用?

Do you know if it is posible to use the element itself with a specific selector like $(this)?

推荐答案

您可以改为:

$(".tags").each(function(){
  var $this = $(this);
  $this.select2({
  width: '220px',
  tags: $this.data('collection')
  });
});

因为在通话期间this并不代表选择器中的元素.

Because during your call this doesn't represent the element in the selector.

这篇关于在select2中使用$(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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