jq Bootstrap multiselect - 必须至少选择一个值

Jq Bootstrap multiselect - At least one value must be selected

我使用 jq bootsrtrap multiselect,我需要一个 select,其中至少有一个值必须 selected。如何操作?

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script>

<select id="select-records" multiple="multiple" class="form-control">
    <option value="0">0</option>
    <option value="50">50</option>
    <option value="500">500</option>
</select>

$('#select-records').multiselect({
  maxHeight: 400,
  buttonWidth: '100%',
  onChange: function (element, checked) {

  // ...

  }
});

$("#select-records").val() return selected 值数组

if($("#select-records").val().length == 0){
alert("You must select at least one option");
}

如果你想在 chage 事件中使用

$( "#select-records" ).change(function() {

if($("#select-records").val().length == 0){
alert("You must select at least one option");
}
});