Change label on change DropDownListFor value

2019-08-30 06:38发布

问题:

I use a DropDownListFor and a label text on my page.

<%=Html.DropDownListFor(x => x.MotiveClientType,
                        new SelectList(
                            Model.ClientMotives, "Id", "Label", Model.MotiveClientType),
                        new { id = "ddlMotiveClientType" }
)%>                       

How can I change my label when selecting value on my DropDownListFor?

回答1:

I'm not aware of any functionality native to ASP.NET MVC that will allow you to do this, but doing it with jQuery is a fairly trivial matter. Here's an example of hooking up an event handler to the change event of the dropdown & then setting the label text:

$("#DropDownID").change(function () {
    $("#LabelID").text("Your text here");
});

Just replace the IDs with the relevant values.