jquery dropdownlist值设置为0

我有1个控件如下

Country
@Html.DropDownListFor(model => model.CountryId, new SelectList(Model.CountriesList, "CountryId", "CountryName"), "Please select one", new { style = "width: 250px;" })

我正在使用jquery来清空这个字段

 $('#CountryId').val(''); $('#CountryId').val('0'); $('#CountryId').text(''); $("#CountryId option[value='0']").val(''); $("input[name='CountryId']").val(''); 

我尝试了所有的fabove事情来清空dropdownlist的值,但没有一个帮助。

同样重要的一点。 我在我class级的国家/地区使用必填字段validation器。所以这可能是问题所在。

请告诉我如何将下拉值设置为0;

您的选择列表如何?首先它的值是否为0? 试试这个

 $("#CountryId option:eq(0)").attr('selected', true) 

检查这个FIDDLE

选择下拉列表中的第5项选项

更新的代码

对于select,如果未提及该值且仅存在文本, 则它将该文本视为值

所以这应该适合你的情况

 $("#CountryId").val('Please select one')​ 

检查更新的FIDDLE

创建一个如下所示的模型类

 public class Country { [RegularExpression("([^0]*$)",ErrorMessage="please select country")] public int? Country_ID { get; set; } public List CountryName{ get; set; } } 

现在在你的控制器动作中写下面的代码……

  public ActionResult actionName() { Country model = new Country(); var query = (from a in DbContext.Country select new { C_ID = a.ID, Name = a.Name }).ToList(); model.CountryName = new SelectList(query, "C_ID", "Name").ToList(); model.CountryName.Insert(0, new SelectListItem { Text = "--Select--", Value = "0" }); return View(model); } 

现在装饰你的视图如下

 <%: Html.DropDownListFor(m => m.Country_ID, Model.CountryName, new { id = "drpCountryId", Style = "width:150px;" })%> <%:Html.ValidationMessageFor(m => m.Country_ID, null, new { @class = "field-validation-message" })%> 

我希望这可以帮助你….

检查这个FIDDLE

 $("select#CountryId")[0].selectedIndex = 1; 

HTML FIDDLE

  

您可以使用以下任何一项来清除所选值/设置要选择的第一个值:

 $('#CountryId option').first().prop('selected', true); 

要么:

 $('#CountryId').find('option').prop('selected',false); 

下面的代码将第一个选项“请选择一个”设置为值0

 $("#CountryId option:eq(0)").val(0).attr("selected", true);