问题
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