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 selectmenus in a divand 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 nulloption inside the selecttag:
我也没有标签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 selectlist to null, given that I don't have the nulloption?
鉴于我没有选项,如何将select列表的值设置为?nullnull
回答by RightSaidFred
If you want to show no selection, you should set its selectedIndexto -1.
如果您不想显示任何选择,则应将其设置selectedIndex为-1。
elements[i].selectedIndex = -1;
回答by Blender
You could try setting the element's valueproperty:
您可以尝试设置元素的value属性:
elements[i].value = '';

