如果选中,则禁用第二选择框的选项

时间:2019-02-18 21:48:01

标签: javascript jquery jquery-select2

我有两个选择框,使用select2 js插件。这两个下拉菜单具有相同的选项。如果要在第一个选择框中选择第二个选择框中的选项,我想禁用它,反之亦然。

代码如下:

jQuery(document).ready(function() {
  jQuery('.minimal').select2();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<select class="minimal" name="coin1">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>

<select class="minimal" name="coin2">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>

2 个答案:

答案 0 :(得分:2)

我跳入documentation,这应该可以解决问题:

这是两个选择的一个功能,它使用name属性来区分。

jQuery(document).ready(function() {
  jQuery('.minimal').select2();
});

$('select.minimal').on('select2:select', function(e) {

  let elem;
  elem = ($(this).attr("name") == "coin1") ? 2 : 1; //set the elementIndex

  //reset
  $('select[name="coin' + elem + '"] > option').removeAttr("disabled");
  $('select[name="coin' + elem + '"]').select2();

  const value = $(this).select2('data')[0].text; //select the value
  $('select[name="coin' + elem + '"] > option[value="' + value + '"]').attr("disabled", true);

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<select class="minimal" name="coin1">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>

<select class="minimal" name="coin2">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>

答案 1 :(得分:2)

请参见下面的代码注释:

$(document).ready(function() {
  $('.minimal')
    .select2()
    .change(function() {                      // when changed
      let val = $(this).val();                // save the current value
      $('.minimal').not(this).find('option')  // grab options from other select
        .prop('disabled', function() {        // disable those equal to current value
          return $(this).val() == val;
        })
        .select2();                           // re-establish select2
    });
});

$(document).ready(function() {
  $('.minimal')
    .select2()
    .change(function() {
      let val = $(this).val();
      $('.minimal').not(this).find('option').prop('disabled', function() {
        return $(this).val() == val;
      }).select2();
    });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<select class="minimal" name="coin1">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>

<select class="minimal" name="coin2">
  <option value="btc">btc</option>
  <option value="usd">usd</option>
  <option value="eth">eth</option>
</select>