如何ASP.net的dropdownlist指数从前端设置为0 [英] How to set ASP.net dropdownlist index to 0 from the front-end
本文介绍了如何ASP.net的dropdownlist指数从前端设置为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ASP.net页面下面code:
I have the following code in ASP.net page:
<asp:DropDownList ClientIDMode="Static" ID="ddl1" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
<asp:DropDownList ClientIDMode="Static" ID="ddl2" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
<asp:DropDownList ClientIDMode="Static" ID="ddl3" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList>
<input type="button" id="ClearForm" value="Clear" class="btn1" />
JQuery的来设置索引为0的所有三个的dropdownlist:
JQuery to set the index to 0 for all three dropdownlist:
$(function () {
$("body").on("click", "#ClearForm", function (e) {
e.preventDefault();
$("select#ddl1, select#ddl2, select#ddl3").prop('selectedIndex', 0); //does not set the selected index to 0
alert($("select#ddl1").text()); //displays all the options from #ddl1
});
});
我
如何修改,以便将DropDownList索引被设置为0。
How can I modify so the dropdownlist index is set to 0.
HTML渲染第一个DropDownList的:
HTML render of the first dropdownlist:
<select name="ctl00$BodyPlaceHolder$ddl1" id="ddl1" class="le">
<option value="Select a State">Select a State</option>
<option value="Alabama">AL</option>
<option value="Alaska">AK</option>
<option value="Arizona">AZ</option>
</select>
我尝试以下jQuery和它返回空
:
alert($("select#ddl1").val());
也试过以下,并没有工作:
Also tried the following and it didn't work:
$('select').each(function () {
$(this).find('option:first').prop('selected', 'selected');
});
添加下面的JavaScript:
Added the following JavaScript:
function setSelectedIndex(dropdownlist, selVal) {
var ddl1 = document.getElementById(dropdownlist);
alert(ddl1.selectedIndex);
if (ddl1.length >= selVal) { //if the selected value is greater then 0, the alert is shown but the value is not set back to 0.
ddl1.selectedIndex = selVal;
alert("greater or equal");
}
else {
alert("not greater or equal");
}
}
这似乎是,如果我删除选定的jQuery的正常工作: http://jsfiddle.net/bpbvhsay/
推荐答案
我使用所选的Jquery造成DropDownList的不更新。
I was using the Chosen Jquery which caused the dropdownlist to not update.
这是HTML:
<select name="ctl00$BodyPlaceHolder$ddl1" id="ddl1" class="chose-select le">
<option value="Select a State">Select a State</option>
<option value="Alabama">AL</option>
<option value="Alaska">AK</option>
<option value="Arizona">AZ</option>
</select>
JavaScript代码在选定的索引设置为0:
JavaScript which sets the selected index to 0:
function setSelectedIndex(dropdownlist, selVal) {
var ddl1 = document.getElementById(dropdownlist);
if (selVal < ddl1.selectedIndex) {
ddl1.selectedIndex = selVal;
$(ddl1).val(0).trigger('chosen:updated');
}
}
这是卓有成效的。
这篇关于如何ASP.net的dropdownlist指数从前端设置为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文