ASP .NET MVC - DropDownList OnChange

时间:2013-08-29 11:56:16

标签: asp.net-mvc javascript-events onchange dropdownlistfor

我想在dropdownList中选择一个值时隐藏一些数据。

示例:

当我选择Gender = M时,我不想在我的标题中看到:Mr Only Miss或Madame。

这是我的代码:

     @Html.DropDownListFor(m => m.Gender, new[] {
                           new SelectListItem() {Text = "M", Value = "M"},
                           new SelectListItem() {Text = "F", Value = "F"},
                           }, "---Choose Gender---", new { onchange = "Select();" })

     @Html.DropDownListFor(m => m.Title, new[] {
                           new SelectListItem() {Text = "Mister", Value = "Mr."},
                           new SelectListItem() {Text = "Madame", Value = "Mme."},
                           new SelectListItem() {Text = "Miss", Value = "Miss."}
                            }, "---Choose Title---")

在Javascript部分:

   function Select() {
        // the code.
    }

非常感谢你!

1 个答案:

答案 0 :(得分:5)

您可以在JQuery的帮助下完成此操作,

  function changeGender()
    {
    if($('#Gender').val()=='M')
    {
    $('#Title').html('');
    $('#Title').html('<option value="Mister">Mr.</option>');
    }
    else if($('#Gender').val()=='F')
    {
    $('#Title').html('');
    $('#Title').html('<option value="Madame">Madam</option><option value="Miss">Miss.</option>');
    }
    else
    {
    $('#Title').html('');
    }
return false;
    }

希望这有帮助。

相关问题