Disable dropdown options when max is reached RRS feed

  • Question

  • User444756084 posted

    I have a dropdownlist in my MVC application that I need to disable options if the option selected reaches the maximum allowed. Can someone point me in the right direction? What I have is not working. For example, I can have multiple dropdowns. If I have 50 then each option should only be selected a maximum of 10 times.

    Dropdownlist in view

    @Html.DropDownList("Task",  new SelectList(new List<int> { 0, 1, 2, 3, 4, 5 }, Model.Aero.Tasks.HasValue ? Model.Aero.Task: null), new
           id = "task" + @Model.Aero.Nom_ID,
           onchange = "javascript:updateTask();"@*, id = "task" + @m.AERO.Nom_ID*@})


    function updateTask() {
        var taskddl = "#task_" + nomID;
        task = $(taskddl).val();
        var taskCnt = $('select').length;
        var taskMax = Math.ceil($('select').length / 5);
        var NumOnesVal = $('#numOnes').val();
        var NumOnesSelected = $('select option:selected').val(1).index()+1;
        alert("NUmber of selected:" + NumOnesSelected);
    //trying for option 1
        if (NumOnesVal < taskMax) {
            $("select option[value*=1]").prop('disabled', true);

    Wednesday, January 30, 2019 10:49 PM

All replies

  • User839733648 posted

    Hi gordon1221,

    For example, I can have multiple dropdowns. If I have 50 then each option should only be selected a maximum of 10 times.

    Do you mean the dropdowns have several or mean the multiple seleted dropdowns?

    I'm sorry that I could not understand your requirement cleatly.

    And I did not find the NumOneVal value in your js file, what does it refer to?

    Besides, it seems that you want to get the length of dropdownlist item, I suggest you could modify as below.

        var taskCnt = $('select option').length;
        var taskMax = Math.ceil($('select option').length / 5);

    Best Regards,


    Thursday, January 31, 2019 10:03 AM
  • User444756084 posted

    Hi Jennifer,

    I could have 50 tasks listed and each one will have a dropdown. 

    Thursday, January 31, 2019 1:20 PM
  • User753101303 posted


    In short you assign 50 tasks to 6 "categories" and you can add up to 10 tasks to each category ? When a value is changed in a drop down you could count how many items you have already and disable the corresponding options. You could optimize by keeping track of that as you go.

    Before diving into this and seeing in practice how to do that, I would see if the UI couldn't be changed as this kind of UI could be a bit confusing for users I believe. Maybe using drag/drop to move tasks into one of the six slots shown on the page could be easier to handle for users ? For example they'll see visually which category is full to make room if needed. With dropdowns they may have to select each dropdown in turn to find out which task is where before moving one of those task to another category to make room. Seems less easy.

    Thursday, January 31, 2019 1:31 PM
  • User444756084 posted

    It is not an interface that can be changed at this time. I just need to be able to disable when the options hit the max selected. Thank you though

    Thursday, January 31, 2019 6:46 PM
  • User839733648 posted

    Hi gordon1221,

    I'm still confused about what is NumOnesVal. I could not find the id numOnes in your view.

    Best Regrads,


    Friday, February 1, 2019 9:11 AM