选择选项的Javascript属性

时间:2016-11-29 15:48:08

标签: javascript html

我需要以下代码的一些帮助,我想设置已经选择到textarea但我不想等同/返回值属性但是叙述。对于下面的情况,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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

您需要找到selectedIndex option的文字内容(或HTML内容)。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以按索引this.options[this.selectedIndex]获取所选选项,然后使用.text而不是值来获取选项文字:

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

这会有所帮助。

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>

答案 2 :(得分:0)

您可以更改每个选项的git rebase -i

&#13;
&#13;
value
&#13;
&#13;
&#13;

相关问题