javascript - 将选择选项值设置为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8477785/
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
javascript - set select option value to null
提问by tanya
I have to disable all the select
menus in a div
and also set their value to null
.
我必须禁用 a 中的所有select
菜单div
并将它们的值设置为null
.
I am using the following code to disable the select menus:
我正在使用以下代码禁用选择菜单:
var div_contents =document.getElementById("div1");
var elements = div_contents.getElementsByTagName("select");
for (i=0; i<elements.length; i++) {
elements[i].disabled = true;
}
However, I am not able to set the value of the select tag to null
.
但是,我无法将 select 标记的值设置为null
.
Also I don't have the null
option inside the select
tag:
我也没有标签null
内的选项select
:
<select id="test1" name="test1">
<option value="a">aa</option>
<option value="b">bb</option>
<option value="c">cc</option>
</select>
How can I set the value of a select
list to null
, given that I don't have the null
option?
鉴于我没有选项,如何将select
列表的值设置为?null
null
回答by RightSaidFred
If you want to show no selection, you should set its selectedIndex
to -1
.
如果您不想显示任何选择,则应将其设置selectedIndex
为-1
。
elements[i].selectedIndex = -1;
回答by Blender
You could try setting the element's value
property:
您可以尝试设置元素的value
属性:
elements[i].value = '';