[英]javascript change select option text
Im trying to modify the text of the first select option via javascript. 我试图通过JavaScript修改第一个选择选项的文本。 But it empties the entire select option
但它清空了整个选择选项
<select name='stuff'>
<option value="a"> Pepsi </option>
<option value= "b"> Juice </option>
</select>
<script type="text/javascript">
document.getElementsByName('stuff')[0].innerHTML = "Water";
</script>
您想要从选项集合中读取并修改其中的第一个元素:
document.getElementsByName('stuff')[0].options[0].innerHTML = "Water";
你可以试试这个:
$('select[name=stuff] option:first').html("abcd");
I think this should work: 我认为这应该有效:
<select name='stuff'>
<option id="first" value="a"> Pepsi </option>
<option value= "b"> Juice </option>
</select>
<script type="text/javascript">
document.getElementById("first").innerHTML = "Water";
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.