我使用 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) { // ... }});
1 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
$("#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");
}
});
添加回答
举报
0/150
提交
取消