Saturday, 11 February 2017

MVC - jQuery - How to get selected text and ID from DropDownList in jQuery


Watch on YouTube


here is my view
@model MVCDDLTest.Models.CustomerModel

@using (Html.BeginForm(null, null, FormMethod.Post))
{
    @Html.AntiForgeryToken()


    @Html.DropDownListFor(Model =>Model.SelectedCompanyID,
        new SelectList(Model.Comp, "CompanyID", "CompanyName"), "default value", new { @id = "myID" })
    <input type="submit" value="Save" />
}

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#myID").change(function (evt) {
                alert($("#myID").val());   //Display Selected ID
                alert($("#myID option:selected").text()) //Display Selected Text
            });
        });
    </script>   
}

No comments:

Post a Comment