从到相关的select2

时间:2019-05-09 10:15:26

标签: jquery jquery-select2

我有一些带有两个相关select的代码,分别带有 from 选项和 to 选项:

if($('.select').length){
  $('.select').select2({
    tags: true,
    placeholder: function(){
      if($(this).data('placeholder')){
        $(this).data('placeholder');
      }
    }
  });
  
  
  $('.select-from').change(function(){
    var selecteId = $(this).select2('val');
    
    if($('.select-to').select2('val') <= selecteId) {
      $('.select-to').select2('disable');
    }
    
    //$('.select-to').select2('val', selecteId);
  });
}
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.full.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
  
  <div class="container">
    <div class="filter-col filter-col_room">
      <span class="filter-text mb-4 d-block">
        Among
      </span>

      <div class="d-flex align-items-center">
        <span class="filter-col__label mr-1">
          from
        </span>
        <select name="catalog-filter-from" class="select select-from" id="count-room-from">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
        </select>
        <span class="filter-col__label ml-4 mr-1">
          to
        </span>
        <select name="catalog-filter-to" class="select select-to" id="count-room-to">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
        </select>
      </div>
    </div>
  </div>

对于自定义select,我使用select2插件。

问题:如何连接这些选择,以便在第二个列表中设置的值不能小于第一个?

0 个答案:

没有答案