Using $(this) in select2

僤鯓⒐⒋嵵緔 提交于 2019-12-22 09:56:15

问题


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

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


回答1:


You can do this instead:

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

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



来源:https://stackoverflow.com/questions/17436253/using-this-in-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!