![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
如何通过js动态设置select中option选中
展开全部
获取select标签,设置它的value值和option的value值相同
<select name="" id="test" value="one">
<option value="one">one</option>
<option value="two">two</option>
</select>
<button id="btn">改变选中状态</button>
<script type="text/javascript">
var select = document.querySelector('#test');
var btn = document.querySelector('#btn');
btn.onclick = function () {
if (select.value === 'one') {
select.value = 'two';
} else {
select.value = 'one';
}
}
</script>
<select name="" id="test" value="one">
<option value="one">one</option>
<option value="two">two</option>
</select>
<button id="btn">改变选中状态</button>
<script type="text/javascript">
var select = document.querySelector('#test');
var btn = document.querySelector('#btn');
btn.onclick = function () {
if (select.value === 'one') {
select.value = 'two';
} else {
select.value = 'one';
}
}
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询