JavaScript多个select2在单击事件中不工作

发布于 2025-02-09 15:32:25 字数 1665 浏览 3 评论 0原文

我想单击按钮创建一个多个选择框(Select2)。

每个按钮单击将创建一个使用 select2 功能库的选择框。

我的代码只会出现一个简单的选择框。

 $('.main_select2').select2({
    placeholder: 'Selection Box',
    allowClear: true
  });
$('#add_select').on('click', function() {
    $('.for_select').append( 
    '<div class="c-input c-input--select c-input--icon">'
          + '<select class="select2 main_select2">'
          + '<option>Choose 1</option>'
          + '<option>Choose 2</option>'
          + '<option>Choose 3</option>'
          + '</select>'
      + '</div>');
  })
.main_select2{
  width:200px;
  margin-top:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.0.0/select2.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/2.1.0/select2.min.js"></script>

<button type="button" id="add_select">Add Select2</button>
<div class="for_select">
  <div class="c-input c-input--select c-input--icon">
      <select class="select2 main_select2">
          <option>Choose 1</option>
          <option>Choose 2</option>
         <option>Choose 3</option>
      </select>
  </div>
</div>

是否有任何可能的方法可以使用Select2中的多个类来完成此操作。

谢谢

I want to click button to create an multiple selectbox(select2).

Each button click will create a select box with select2 function libraries.

My code will appear only a simple selectbox.

 $('.main_select2').select2({
    placeholder: 'Selection Box',
    allowClear: true
  });
$('#add_select').on('click', function() {
    $('.for_select').append( 
    '<div class="c-input c-input--select c-input--icon">'
          + '<select class="select2 main_select2">'
          + '<option>Choose 1</option>'
          + '<option>Choose 2</option>'
          + '<option>Choose 3</option>'
          + '</select>'
      + '</div>');
  })
.main_select2{
  width:200px;
  margin-top:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.0.0/select2.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/2.1.0/select2.min.js"></script>

<button type="button" id="add_select">Add Select2</button>
<div class="for_select">
  <div class="c-input c-input--select c-input--icon">
      <select class="select2 main_select2">
          <option>Choose 1</option>
          <option>Choose 2</option>
         <option>Choose 3</option>
      </select>
  </div>
</div>

Is there any possible ways to do this with multiple class in select2.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2025-02-16 15:32:25

您需要将选择作为select2初始化,就像在页面加载中存在的一样。您的代码附加新的选择,但它不会将其初始化为Select2。

这是一个工作片段。我在第一个,基本选择的情况下添加了hidden css类,因为您可能不希望看到该类别。

let count = 0;                         // Track how many copies we have
let $template = $('.c-input--select'); // The HTML you want to copy each time
let $container = $('.for_select');     // Where the copies should be added

$('#add_select').on('click', function() {

    // Increment our counter
    count++;

    // Create a copy of your base HTML/select
    let $copy = $template.clone();
    
    // Find the select in the div, and give it an id so we can find it
    $copy.find('select').attr('id', count);
 
    // Append it
    $container.append($copy);
    
    // Initialise it as a select2, using the id we just gave it to find it
    $('#' + count).select2();
});
.hidden {
    display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.0.0/select2.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/2.1.0/select2.min.js"></script>

<button type="button" id="add_select">Add Select2</button>
<div class="for_select">
  <div class="c-input c-input--select c-input--icon">
      <!-- add hidden class so the template is not visible -->
      <select class="select2 main_select2 hidden">
          <option>Choose 1</option>
          <option>Choose 2</option>
         <option>Choose 3</option>
      </select>
  </div>
</div>

You need to initialise your select as a Select2, just as you would for one that exists on page load. Your code appends a new select, but it doesn't initialise it as a Select2.

Here's a working snippet. I've added a hidden CSS class to your first, base select, since you probably don't want that visible.

let count = 0;                         // Track how many copies we have
let $template = $('.c-input--select'); // The HTML you want to copy each time
let $container = $('.for_select');     // Where the copies should be added

$('#add_select').on('click', function() {

    // Increment our counter
    count++;

    // Create a copy of your base HTML/select
    let $copy = $template.clone();
    
    // Find the select in the div, and give it an id so we can find it
    $copy.find('select').attr('id', count);
 
    // Append it
    $container.append($copy);
    
    // Initialise it as a select2, using the id we just gave it to find it
    $('#' + count).select2();
});
.hidden {
    display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.0.0/select2.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/2.1.0/select2.min.js"></script>

<button type="button" id="add_select">Add Select2</button>
<div class="for_select">
  <div class="c-input c-input--select c-input--icon">
      <!-- add hidden class so the template is not visible -->
      <select class="select2 main_select2 hidden">
          <option>Choose 1</option>
          <option>Choose 2</option>
         <option>Choose 3</option>
      </select>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文