Javascript 使用 getElementsByName() 设置选项值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2343889/
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
Set option value with getElementsByName()
提问by volvox
Having this fieldset:
有这个字段集:
<fieldset>
<legend>[*death]</legend>
<select name=death style="width: 120px">
<option value=Dead>[*died]
<option value=NotDead>[*alive]
<option value="" selected>-
</select>
</fieldset>
i want to set the [2].valueto "-".
我想设置[2].value为"-".
i have tried without any success:
我试过没有任何成功:
document.getElementsByName('death')[2].checked = 'true';
document.getElementsByName('death')[2].value = '-';
Same kind of code works fine for radio boxes, checked boxes or other inputs in the form. How to do it with the option select (which is not an input)?
相同类型的代码适用于表单中的单选框、复选框或其他输入。如何使用选项 select (这不是输入)来做到这一点?
Thanks
谢谢
[EDIT] of course, appropriate fieldset is:
[编辑] 当然,适当的字段集是:
<fieldset>
<legend>[*death]</legend>
<select name="death" style="width: 120px">
<option value="Dead">[*died]</option>
<option value="NotDead">[*alive]</option>
<option value="" selected>-</option>
</select>
</fieldset>
thanks.
谢谢。
回答by moribvndvs
It's a little bit unclear what you're asking. Are you simply asking to make the option at index 2 selected?
有点不清楚你在问什么。您是否只是要求选择索引 2 处的选项?
document.getElementsByName('death')[0].selectedIndex = 2;
Or, are you asking to change the value of option at index 2?
或者,您是否要求更改索引 2 处的选项值?
var d = document.getElementsByName('death')[0];
d.options[2].value = '-';
回答by brabster
You need to manipulate the selectedproperty of your select object, try
您需要操作selected选择对象的属性,请尝试
document.getElementsByName('death')[0].selectedIndex = 1;
document.getElementsByName('death')[0].selectedIndex = 1;
In english, this reads "set the selected option to the second option in the first element in the document with name 'death'".
在英语中,这读作“将选定的选项设置为文档中名为‘death’的第一个元素中的第二个选项”。
Fixing your HTML might make the results of your javascript more predictable. Close your tags, quote your attribute values, as follows:
修复您的 HTML 可能会使您的 javascript 的结果更可预测。关闭您的标签,引用您的属性值,如下所示:
<fieldset>
<legend>[*death]</legend>
<select name="death" style="width: 120px">
<option value="Dead">[*died]</option>
<option value="NotDead">[*alive]</option>
<option value="" selected>-</option>
</select>
</fieldset>
回答by Ju Nogueira
you can do this using jQuery... it's easy...
你可以使用 jQuery 做到这一点......这很容易......
j("#death").val(2)
回答by user3724032
Here's an alert example of how to access your specific option values with getElementsByName
这是如何使用getElementsByName访问特定选项值的警报示例
alert(document.getElementsByName('death')[0].options[0].value); // will return Dead
alert(document.getElementsByName('death')[0].options[1].value); // will return NotDead
回答by kevingessner
document.getElementsByName('death')[2]returns the third element named death- but you only have one element with that name. Instead, you want the first element named death(i.e. the one at index 0), and then you want its third option: document.getElementsByName('death')[0].options[2].value = ...
document.getElementsByName('death')[2]返回命名的第三个元素death- 但您只有一个具有该名称的元素。相反,您需要命名的第一个元素death(即索引 0 处的元素),然后您需要它的第三个选项:document.getElementsByName('death')[0].options[2].value = ...

