簡體   English   中英

限制easyui組合框中的選擇數量

[英]Limit the number of selections in easyui combobox

我有多個選擇的easyui組合框。 我對可以選擇的項目數設置了限制,因此,如果您嘗試選擇的數量超出限制,則會提醒用戶並且不允許選擇該項目。 這是我的代碼。

$("#assessment_content_items_select_standards").combobox({
        data: $rootScope.assessmentItemsFilters.selectedStandards,
        valueField: 'value',
        textField: 'label',
        multiple: true,           
        onSelect: function (standard)
        {
            var selectedStandardsCount = 0;
            angular.forEach($rootScope.assessmentItemsFilters.selectedStandards, function (stan) {
                if (stan.selected == true) {
                    selectedStandardsCount++;
                }
            });

            if (selectedStandardsCount >= 25) {
                alert("You have reached the maximum selected standards allowed of 25");
            }
            else {
                standard.selected = true;
            }
        },
        onUnselect: function (standard) {
            standard.selected = false;
        }                       
    });

在onSelect方法中,我得到了選定的計數,如果它大於25,它只會發出警報,並且不會將該選擇的selected設置為true。

我遇到的問題是,即使數據顯示為選中狀態,UI仍將項目顯示為選中狀態:false。 我嘗試了以下取消選擇的選項,但沒有區別。

standard.selected = false
$("#assessment_content_items_select_standards").combobox('unselect', standard);

我的問題是如何停止選擇選項,或者是否有更好的方法限制下拉菜單?

試試這個:

$('#assessment_content_items_select_standards').combobox({
    onSelect: function (standard) {
        var count = $("div.combobox-item-selected").length;
        if (count > 25) {
            alert("You can only choose 25."); 
            $('#assessment_content_items_select_standards').combobox('unselect', standard. value);
        }
    }
});

參考文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM