如何在 Javascript 中更改 DropDownList Selected 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10029276/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:33:37 来源:igfitidea点击:
How to change DropDownList Selected value in Javascript?
提问by karthik
how to change the selected value in javascript and get the selected value in codebehind page? AutoPostBack is set to false.
如何更改javascript中的选定值并在代码隐藏页面中获取选定值?AutoPostBack 设置为 false。
回答by Coder
You can change it like this:
你可以像这样改变它:
var ddl = document.getElementById('ddl-id');
var opts = ddl.options.length;
for (var i=0; i<opts; i++){
if (ddl.options[i].value == "some-value"){
ddl.options[i].selected = true;
break;
}
}
回答by Avi Y
//setting the value of a drop down list
document.getElementById('my_drop_down').selectedIndex=2;