Bootstrap Multi-Select-无法启用选择取消选中/取消选中所有选项时

问题描述 投票:0回答:1

我有两个选项,允许使用bootstrap select plugin- services和payment_type进行多项选择。我想要的是当取消选中/取消选中服务中的所有选项时,pay_type变为启用状态。注意:代码中的禁用工作正常,但字段的启用不起作用。我在这里做错了吗?

此外,最初禁用payment_type和服务,但是通过选择另一个字段来启用。

<select id="services" class="form-control" multiple="multiple" disabled>
       <option value="tire_rotation">Tire Rotation</option>
       <option value="oil_change">Oil Change</option>
       <option value="brake_check">Brake Check</option>
 </select>


 <select id="payment_type" class="form-control" multiple="multiple" disabled>
       <option value="cash">Cash</option>
       <option value="check">Check</option>
       <option value="credit_card">Credit Card</option>
       <option value="debit_card">Debit Card</option>
 </select>
$(document).ready(function() {
       $('#services').multiselect({
           buttonWidth: '375px',
           nonSelectedText: 'Select...',
           dropLeft: true,
           includeSelectAllOption: true,
           numberDisplayed: 1,
           onChange: function() {

               var servicesSelected = $("#services :selected").length;

               if ($(servicesSelected.length != 0)) {
                  $('#payment_type').multiselect('disable');
                  $('#payment_type').multiselect("deselectAll", false).multiselect("refresh");

               } 
               else {
                    $('#payment_type').multiselect('enable');
               }
           }
       });
   });

预期结果:取消选中所有服务后,将启用payment_type select。

javascript jquery bootstrap-multiselect
1个回答
1
投票

这将有效......

 $(document).ready(function() {
  $('#services').multiselect({
    buttonWidth: '375px',
    nonSelectedText: 'Select...',
    dropLeft: true,
    includeSelectAllOption: true,
    numberDisplayed: 1,
    onSelectAll: function(checked) {
      enableDisablePaymentType(checked);
    },
    onChange: function() {
      var servicesSelected = $("#services :selected").length > 0 ? true : false;
      enableDisablePaymentType(servicesSelected);
    }
  });
});

function enableDisablePaymentType(servicesSelected) {
  if (servicesSelected) {
    $('#payment_type').multiselect('disable');
    $('#payment_type').multiselect("deselectAll", false).multiselect("refresh");
  } else {
    $('#payment_type').multiselect('enable');
  }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.