简体   繁体   English

在使用JQuery加载的元素上执行Select2

[英]Select2 on elements loaded with JQuery

I'm using select2 on a project, and is working as it should, initiating the select2 using: 我在一个项目上使用select2,并且正在按预期方式工作,使用以下命令启动select2:

$(function() {
  $(".elegible").select2();
});

The issue comes in when I'm trying to call de function to elements loaded by JQuery, I don't know how to call that function. 当我尝试对JQuery加载的元素调用de函数时,就会出现问题,我不知道如何调用该函数。 I've tried "on" but it does not load the selects with the select2. 我试着“上”,但它不会加载的selects与选择2。 The only thing I found is this: 我发现的唯一结果是:

$(document).ajaxComplete(function(e, xhr, settings){
    $(".elegible").select2();
});

But this calls that function every time an ajax call is completed, even in those cases when I'm not needing the select2. 但这每次ajax调用完成时都会调用该函数,即使在我不需要select2的情况下也是如此。 My question is: How can I call the select to on elements loaded later? 我的问题是:如何在以后加载的元素上调用select to? Is the method that I'm using right now valid? 我现在使用的方法是否有效? Can I improve that? 我可以改善吗?

Thanks! 谢谢!

Adding Info: I call the new content by getting it from another page - which loads the select - using ajax: 添加信息:我通过使用ajax从另一个页面(加载选择内容)获取新内容来进行调用:

$.get('detalle_miembro/' + this.id)
  .done(function(result) {
    $('#detalle_miembro').html(result);
  });

And placing it, in a div: <div id="detalle_miembro"></div> 并将其放在div中: <div id="detalle_miembro"></div>

Here is how you can call select on just the new content added : 这是您可以仅对添加的新内容调用select

$.get('detalle_miembro/' + this.id).done(function(result) {
    $('#detalle_miembro').html(result).find(".elegible").select2();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM