使用Javascript设置Asp:下拉列表
Using Javascript to set a Asp:Dropdownlist
我尝试了各种方法来设置下拉列表无效。在开发人员工具中,我可以看到selectedIndex
的适当更改,我可以看到选项节点从选定的false
更改为选定的true
。但是当弹出模式打开时,下拉列表显示第一个选项,它是空白的。如果我关闭并重新打开弹出窗口,它将显示我手动选择的任何内容。我如何以编程方式设置下拉列表值?
标记
<%--Address Popup--%>
<div id="location_modal" class="reveal-modal modal_location" data-reveal>
<fieldset>
<legend>Locations(Update/New)</legend>
<div class="row">
<div class="large-4 columns">
<asp:Label ID="Label4" runat="server">Location:*</asp:Label>
<asp:DropDownList ID="ddLocationNames" runat="server" class="input_ddllocationName"></asp:DropDownList>
</div>
Js
locationNameId = 'MainContent_lvAddresses_lblAddressLocation_' + g_index
locationName = document.getElementById(locationNameId).innerHTML;
var select = document.getElementById("MainContent_ddLocationNames");
for (var i = 0; i <select.options.length; i++){
if(select.options[i].innerHTML == locationName){
//select.selectedIndex = i;
//$(".input_ddllocationName").selectedIndex = i;
select[i].selected = true;
}
else{
select[i].selected = false;
}
}
Droplistid: MainContent_ddLocationNames
1. selected="selected" value=""
2. value="1">TESTING
3. value="2">TESTING AGAIN
4. value="3">MY FAVORITE LOCATION
5. value="4">MGM GRAND BALLROOM A
如果页面上有jQuery:
$(".input_ddllocationName").val(locationName);
或locationNameId,取决于哪个是选项的值属性
如果你使用的是核心JavaScript,那么这应该可以工作。
locationName = document.getElementById(locationNameId).innerHTML;
var select = document.getElementByClassName("input_ddllocationName")[0];
for (var i = 0; i <select.options.length; i++){
if(select.options[i].innerHTML == locationName){
select.selectedIndex = i;
// OR select.value = select.options[i].value;
}
else{
select[i].selected = false;
}
}
由于基础,下拉列表有一些'特殊'标记。
在Js中,我必须添加一些额外的代码行。感谢Google开发工具!//Find out which row should be selected
var indexSave = 0;
for (var i = 0; i<select.options.length; i++){
if(select.options[i].innerHTML == locationName){
indexSave = i;
break;
}
}
//Set the current node to the Location Name
$('#MainContent_ddlLocationName').next().children().first().text(locationName);
//Remove all selections
$('.input_ddlLocationName ul li').each(function() {
$(this).removeClass("selected");
});
//Select the Location Name based on index
var selectLi = indexSave + 1;
$('.input_ddlLocationName ul li:nth-child(' + selectLi + ')').addClass("selected");
相关文章:
- 如何使用javascript将值引导下拉列表设置为隐藏值asp.net
- jQuery 以选择 asp.net 中的下拉列表控件
- 禁用基于下拉列表选择 asp.net JavaScript 启用文本框
- 在 asp.net 中用javascript填充国家和城市下拉列表
- 如何使用 asp.net 中的另一个下拉列表更改下拉列表选定的索引和可见性
- 经典 ASP - 下拉列表:选择与第一个下拉列表具有相同值的所有下拉列表
- ASP.NET 显示单选按钮列表和下拉列表的进度条/加载
- 由vbscript下拉列表填充的经典asp-javascript数组
- ASP.NET MVC级联下拉列表列出Javascript问题
- 在JavaScript中更改下拉列表索引后,ASP.Net提交按钮单击事件不起作用
- 在asp.net mvc中,从第二下拉列表中选择值后,第二下拉菜单不显示
- 通过javascript ASP.NET MVC下拉列表调用按钮
- 如何在 javascript onchange 应用 asp.net 时以编程方式更改下拉列表中的选定项
- 如何防止 ASP.NET 下拉列表在选择ON选项时触发其onclick事件
- 如何将 JavaScript 添加到 Gridview 的下拉列表(模板归档 ASP.NET
- ASP 剑道下拉列表默认值未使用定义的模板
- Asp.net MVC4 中使用 Json 和 Jquery 的级联下拉列表未填充
- asp.net MVC 从多个下拉列表中获取值
- 级联下拉列表,无需在 asp.net 中重新加载页面
- 从 XML 列表填充的 ASP 下拉列表如何添加工具提示