Javascript HTML/JS:如何使用 JS 更改选择类型的选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8716064/
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-24 07:11:40  来源:igfitidea点击:

HTML/JS: How to change option value of select type using JS

javascripthtmloption

提问by ohmyzZz

I've been trying to figure out how to set a value to a an option in a select type. But no matter how much I try to understand I just can't (feels ashamed)..

我一直在试图弄清楚如何为选择类型中的选项设置值。但无论我多么努力去理解我都做不到(感到羞耻)。

So I was hoping that you guys could help me since you've helped me so many times before.. :)

所以我希望你们能帮助我,因为你们之前已经帮助过我很多次了.. :)

Let's say we have:

假设我们有:

<select name="box" id="test">    
<option value='tval'>Content</option>

shouldn't this next code change the text from 'Content' to 'box'?

下一个代码不应该将文本从“内容”更改为“框”吗?

function changeContent(form){
form.document.getElementById('test').options['tval'].value = 'box';
}

or am I completely wrong here? I've looked up so many different articles but no one could help me understand how to do this..

还是我在这里完全错了?我查了很多不同的文章,但没有人能帮助我理解如何做到这一点..

Thanks guys!

谢谢你们!

回答by Richard A

If You need to change the text rather than the value;

如果您需要更改文本而不是值;

function changeContent(){
     document.getElementById('test').options[0].text = 'box';
}

To set both the value and text;

设置值和文本;

function changeContent(){
     var opt= document.getElementById('test').options[0];
     opt.value =  'box';
     opt.text = 'box';
}

回答by Heldraug

No, valueis the actual value of the option element, what will be sent when the user submits the form. What you are trying to access is the HTML of the option element, you would have to access it with something like:

不是,value是option元素的实际值,用户提交表单时会发送什么。您尝试访问的是选项元素的 HTML,您必须使用以下内容访问它:

form.document.getElementById('test').options['tval'].innerHTML='box'

回答by Pavan

You should use index of the option here. Here is the working example http://jsfiddle.net/LaMJ9/

您应该在此处使用该选项的索引。这是工作示例http://jsfiddle.net/LaMJ9/

Code

代码

document.getElementById('test').options[0].value = 'box'; 

Edit

编辑

addded alerts for previous and new values at jsfiddle http://jsfiddle.net/LaMJ9/1/

在 jsfiddle http://jsfiddle.net/LaMJ9/1/上添加了以前和新值的警报

回答by PAULDAWG

That will change the value from 'tval' to 'box'

这会将值从“tval”更改为“box”

I think what you are looking for is the inner text of the option.

我认为您正在寻找的是选项的内部文本。

W3CSchools.com has an example of what you are looking for in javascript. You did want javascript, correct?

W3CSchools.com 有一个示例,说明您在 javascript 中寻找的内容。你确实想要javascript,对吗?

http://w3schools.com/js/tryit.asp?filename=try_dom_option_settext

http://w3schools.com/js/tryit.asp?filename=try_dom_option_settext

回答by nav

Drop the form.part and use:

放下form.零件并使用:

document.getElementById('test').options[0].innerHTML = 'box'; 

See http://jsfiddle.net/hMqFE/

http://jsfiddle.net/hMqFE/