javascript 在java脚本中获取下拉列表中所选项目的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15565185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To get the text of the selected item in the dropdown list in java script
提问by Nuthan Gowda
I want to get the text of the selected item in the drop down list. I tried the following code
我想在下拉列表中获取所选项目的文本。我尝试了以下代码
var e = document.getElementById("ddlTime.ClientID");
var selectedTime = e.options[e.selectedIndex].text;
but it gives me "Unable to get value of the property 'options': object is null or undefined"
但它给了我“无法获得属性'选项'的值:对象为空或未定义”
can any one please help me out with this ?
任何人都可以帮我解决这个问题吗?
采纳答案by Phong Vo
回答by Habib
You have to specify <%= %>
你必须指定 <%= %>
var e = document.getElementById("<%= ddlTime.ClientID%>");
回答by Raj
JavaScript
JavaScript
function WeekChange() {
var Week = window.document.getElementById("ddWeek").value;
if (Week != "--Select--") {
window.document.getElementById("txtDetect").value = "Every " + Week;
}
else {
window.document.getElementById("txtDetect").value = " ";
}
}
<asp:DropDownList ID="ddWeek" runat="server" onchange="javascript:WeekChange();">