Jquery 如何在MVC中将下拉菜单重置为零索引?
在我的mvc项目中,我在div中绑定了一个下拉列表,如下所示Jquery 如何在MVC中将下拉菜单重置为零索引?,jquery,html,asp.net-mvc,Jquery,Html,Asp.net Mvc,在我的mvc项目中,我在div中绑定了一个下拉列表,如下所示 @{ List<SelectListItem> lsqty = new List<SelectListItem>(); for (int i = 1; i <= 10; i++) { SelectListItem sl = new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selecte
@{
List<SelectListItem> lsqty = new List<SelectListItem>();
for (int i = 1; i <= 10; i++)
{
SelectListItem sl = new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = false };
lsqty.Add(sl);
}
}
Qty: @Html.DropDownList("ddlQty", lsqty, new { style = "width:30px", @class = "positive-integer" })
@{
列表lsqty=新列表();
对于(int i=1;i如果通过代码绑定下拉列表,则在按钮中单击事件设置
DropDownList.SelectedIndex = 0 ;
或者,如果您是静态绑定它,则只需使用该方法即可
DropDownList.ClearSelection();
试试这个:
document.getElementById("<%#mydropdownlist.ClientID%>").value = 0;
document.getElementById(“”)。值=0;
或
document.getElementById(“”)。selectedIndex=0;
我认为您希望值从0
值开始,因此您必须稍微更改for循环,其余的我认为您可以从中获得以更改所选索引
逻辑
@{
List<SelectListItem> lsqty = new List<SelectListItem>();
for (int i = 0; i <= 10; i++) // Starting loop from ZERO *******
{
SelectListItem sl = new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = false };
lsqty.Add(sl);
}
}
Qty: @Html.DropDownList("ddlQty", lsqty, new { style = "width:50px", @class = "positive-integer" })
PS:由于您正在构建ASP.NET MVC应用程序
语法不适用,因此直接使用逻辑部分中的id
@Html.DropDownList(“ddlQty”…您的意思是希望选项中有0
值吗?是的,ddl重置,从索引0开始。有任何答案对您有帮助吗?如果有,请接受有用的答案。我在ajax函数中使用了此选项,但它不起作用。document.getElementById(“”).selectedIndex=0;使用它可能会对您有所帮助。@Sandy您提供的代码不适用于MVC应用程序,因为没有任何ClientID
和DropDownList
或控件
概念,它纯粹是HTML片段、帮助函数和JavaScripts。。
document.getElementById("<%#mydropdownlist.ClientID%>").selectedIndex = 0;
@{
List<SelectListItem> lsqty = new List<SelectListItem>();
for (int i = 0; i <= 10; i++) // Starting loop from ZERO *******
{
SelectListItem sl = new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = false };
lsqty.Add(sl);
}
}
Qty: @Html.DropDownList("ddlQty", lsqty, new { style = "width:50px", @class = "positive-integer" })
<select style="width:50px" name="ddlQty" id="ddlQty" class="positive-integer">
<option value="0">0</option> //New Option value ZERO
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
document.getElementById("ddlQty").selectedIndex = 0;