简体   繁体   English

检查下拉列表中的选定值

[英]check the selected value in drop down list

I have the following drop down list. 我有以下下拉列表。 How can I check the selected value BEFORE saving the data into the module? 在将数据保存到模块之前,如何检查所选值? I want to use the selected value in setting the values of Neighborhood's drop down list. 我想在设置“邻居”下拉列表的值时使用选定的值。

        @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> 

With jQuery, you can try 使用jQuery,您可以尝试

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

If you are not using jQuery, the vanilla javascript way would be 如果您不使用jQuery,则使用普通的javascript方法是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM