Select2 - 在 select 上,从下一个 select 元素中删除禁用的属性

Select2 - On select, remove disabled attribute from next select element

我正在使用 Select2 来设置我的 select 元素的样式,我想拥有它以便最初禁用除第一个下拉列表之外的所有内容,然后当一个选项为 select 时从第一个下拉列表中编辑,禁用属性从下一个下拉列表中删除(强制用户按顺序制作他们的 select 离子)。这是我拥有的:

$('select').select2();
$('select').prop('disabled', true);
$('select:first-of-type').prop('disabled', false);

$('select').on('select2:select', function(e) {
  $(this).next('select').prop('disabled', false); // remove disabled prop from next select
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

不幸的是 $(this).next('select') 似乎没有针对下一个元素。

$('select').select2();
$('select').prop("disabled", true);
$('select').eq(0).prop("disabled", false);
$('select').on('select2:select', function(e) {
  var index = $(this).index('select');
  $('select').eq(index + 1).prop("disabled", false); // remove disabled prop from next select
});

使用 .nextAll() and .first() instead of .next(),它仅“[获取] 匹配元素集中每个元素的紧随其后的兄弟”,并且可选地 过滤器 通过选择器。

只是 .next() 不起作用的原因是因为 select2 在 <select> 之间注入了一个跨度。

$('select').select2();
$('select').prop('disabled', true);
$('select:first-of-type').prop('disabled', false);

$('select').on('select2:select', function(e) {
  $(this).nextAll('select').first().prop('disabled', false); // remove disabled prop from next select
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

$('select').select2({allowClear: true});
$('select').prop('disabled', true);
$('select:first-of-type').prop('disabled', false);

$('select').on('select2:select', function(e) {
  $(this).nextAll('select').first().prop('disabled', false); // remove disabled prop from next select
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>

<select>
  <option>Test 1</option>
  <option>Test 2</option>
  <option>Test 3</option>
  <option>Test 4</option>
</select>