1

I have html page with a dozen dropdownlinst

One of them is

@Html.DropDownListFor(model => model.PrimaryNetworkInt, new SelectList(ViewBag.AvailableNetworks, "ID", "Name"), "Offline")

Second one store some other value, and when I change it to "AAA" I must disable in the fist one ability to choose NULL-oprtion (i.e. "Offline") (and return it back if was selected something else)

Select logic works fine:

$('#SecondDDList').change(function () { SecondDDListChanged(); });

var SecondDDListChanged() = function(){
    //...
    if ($('#SecondDDList').val()==-1){ //-1 i.e == "AAA" in my example
        //Here i need logic to disable NULL selection
    } else {
        //Here i need enable NULL option
    }
}

What is better way to do it? May be something like:

$("#PrimaryNetworkInt option[value=null]").attr("disabled", "disabled");

Any suggestion?

3 Answers 3

1

You can use the methods .show() and .hide() for be shure the user can (or can not) select the value

$("#PrimaryNetworkInt").change(function() {
    if ($(this).val()==-1){ //-1 i.e == "AAA" in my example
        $("#PrimaryNetworkInt option[value='null']").hide();
    } else {
        $("#PrimaryNetworkInt option[value='null']").show();
    }

});
Sign up to request clarification or add additional context in comments.

Comments

1
jQuery(function () {
    jQuery('#SecondDDList').find("option").each(function () {
        if (jQuery(this).val() == -1 || jQuery(this).val() == "null" ) {
            jQuery(this).attr("disabled", true);
        }
    });
});

Comments

0

To disable particular option just set its disabled attribute.

var SecondDDListChanged() = function(){
    //...
    if ($('#SecondDDList').val()==-1){ //-1 i.e == "AAA" in my example
        $("#optionId").attr("disabled", "disabled");
    } else {
        $("#optionId").removeAttr("disabled", "disabled");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.