简体   繁体   English

选择选项的Javascript属性

[英]Javascript attributes for select option

I need some assistance with the code below, I want to set what has been chosen to the textarea but I dont want to equate/return the value attribute but the narration. 我需要以下代码的帮助,我想设置已选择的文本区域,但我不想等于/返回值属性,而是叙述。 for the case below the textarea is getting value 1 and I want it to get the narration Line One 在下面的textarea的情况下获得价值1,我想让它变得叙述一号线

 var myOption = document.getElementById('priority1'); var myStrategy = document.getElementById('strategy1'); myStrategy.onchange = function () { myOption.value = this.value; } 
 <textarea id="priority1" name="priority1"></textarea> <select id="strategy1" name="strategy1"> <option value=""></option> <option value="1">Line one</option> <option value="2">Line Two</option> <option value="3">Line Three</option> </select> 

You need to find the selectedIndex 's option 's text content (or HTML Content). 您需要找到selectedIndex option的文本内容(或HTML内容)。

 <textarea id="priority1" name="priority1"></textarea> <select id="strategy1" name="strategy1"> <option value=""></option> <option value="1">Line one</option> <option value="2">Line Two</option> <option value="3">Line Three</option> </select> <script type="text/javascript"> var myOption = document.getElementById('priority1'); var myStrategy = document.getElementById('strategy1'); myStrategy.onchange = function() { myOption.value = this.querySelectorAll('option')[this.selectedIndex].innerHTML; } </script> 

You could get the selected option by index this.options[this.selectedIndex] then get the option text using .text instead of value : 您可以通过索引this.options[this.selectedIndex]获得所选选项,然后使用.text代替value获得选项文本:

myStrategy.onchange = function () {
    myOption.value = this.options[this.selectedIndex].text
}

Hoep this helps. 有助于此。

 var myOption = document.getElementById('priority1'); var myStrategy = document.getElementById('strategy1'); myStrategy.onchange = function () { myOption.value = this.options[this.selectedIndex].text } 
 <select id="strategy1" name="strategy1"> <option value=""></option> <option value="1">Line one</option> <option value="2">Line Two</option> <option value="3">Line Three</option> </select> <br><br> <textarea id="priority1" name="priority1"></textarea> 

You can change the value of each option 您可以更改每个选项的value

 <!DOCTYPE html> <html> <head lang="en"> <title>Testing Code</title> </head> <textarea id="priority1" name="priority1"></textarea> <select id="strategy1" name="strategy1"> <option value=""></option> <option value="Line one">Line one</option> <option value="Line two">Line Two</option> <option value="Line three">Line Three</option> </select> <body> <script type="text/javascript"> var myOption = document.getElementById('priority1'); var myStrategy = document.getElementById('strategy1'); myStrategy.onchange = function () { myOption.value = this.value; } </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM