检查下拉列表中的选定值

时间:2015-04-04 16:33:37

标签: c# asp.net-mvc razor

我有以下下拉列表。在将数据保存到模块之前,如何检查所选值?我想在设置邻域下拉列表的值时使用所选值。

        @Html.DropDownListFor(model => model.City, new SelectList(
          new List<Object>{
               new { value = "Abha", text = "Abha" },
               new { value = "Al Qunfudhah", text = "Al Qunfudhah" },
               new { value = "Al-Kharj", text = "Al-Kharj" },
               new { value = "Al-Ahsa", text = "Al-Ahsa" },
               new { value = "Buraidah", text = "Buraidah" },
               new { value = "Dammam", text = "Dammam" },
               new { value = "Ha'il", text = "Ha'il"},
               new { value = "Hafar Al-Batin", text = "Hafar Al-Batin" },
               new { value = "Jazan", text = "Jazan" },
               new { value = "Jeddah", text = "Jeddah" },
               new { value = "Jubail", text = "Jubail" },
               new { value = "khobar", text = "khobar" },
               new { value = "Khamis Mushait", text = "Khamis Mushait" },
               new { value = "Mecca", text = "Mecca" },
               new { value = "Medina", text = "Medina" },
               new { value = "Najran", text = "Najran" },
               new { value = "Qatif", text = "Qatif" },
               new { value = "Riyadh", text = "Riyadh" },
               new { value = "Tabuk", text = "Tabuk" },
               new { value = "Ta'if", text = "Ta'if" },
               new { value = "Yanbu", text = "Yanbu" }
          },"value", "text", "Jeddah"))

        @Html.ValidationMessageFor(model => model.City)
    </div>
</div> 

1 个答案:

答案 0 :(得分:1)

使用jQuery,您可以尝试

$('#City').val();

如果您不使用jQuery,那么vanilla javascript方式将是

var element = document.getElementById("City");
var strVal = element.options[element.selectedIndex].value;