简体   繁体   中英

Change disabled select(dropdownlist) based on another select(dropdownlist) value

Basically what I'm trying to do is self-explanatory in the title. Here's a snippet of code which enables the dropdown/select so it can be changed via client, but it will not disable after change:

function setLocVals(passedVal) {

                $('#DropDownList1').removeAttr("disabled");                    
                $('#DropDownList1').val(passedVal);
                $('#DropDownList1'').attr("disabled", "disabled");
}

Is this even possible? Or am I looking at this completely the wrong way? I could change to a textbox, but this is a dropdown containing US states. If there's a record for the user then the selection is restricted for the user, if no record exists, then they can select.

Just put a condition when you have to disable. See this fiddle

Use this approach.

First set the value in option.

Then retrieve the current value of the option and check if it is set to the one than you have passed then put the condition to disable else not

 function setLocVals(passedVal) {


            $('#DropDownList1').val(passedVal);

             var value = $('#DropDownList1').val();
             if(value == passedVal){
                    $('#DropDownList1').attr("disabled", "disabled");
                }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM