Jq Bootstrap 多选 - 必须至少选择一个值

我使用 jq bootsrtrap 多选,我需要一个必须选择至少一个值的选择。怎么做?


<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) {


  // ...


  }

});



慕雪6442864
浏览 264回答 1
1回答

婷婷同学_

$("#select-records").val() 返回所选值的数组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");}});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript