Thursday, May 26, 2011

Dropdownlist callback using jQuery

@model TestDropdownList.Models.Person

<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.CountryCode, "Country")

    @Html.DropDownListFor(x => x.CountryCode,
            new Dictionary<string, string>() { { "PH", "Philippines" }, { "CN", "China" }, { "CA", "Canada" }, { "JP", "Japan" } }
            .Select(x => new SelectListItem { Value = x.Key , Text = x.Value} ) )
}


<script type="text/javascript">
    $(function () {
        $('#CountryCode').change(function () {
            // alert('hello');
            alert($(this).val() + ": " + $('option:selected', $(this)).text());

        });
    });
</script>

No comments:

Post a Comment