查找哪个< Option>在< select>中选择(没有JQuery)
问题描述:
我具有以下元素:
<select id="color" name="colorId" class="btn secondary">
<option value="347366" selected="selected">Purple</option>
<option value="56634">White</option>
</select>
我想找到选择的选项:
以下仅给我默认值:
document.querySelector('#color option[selected="selected"]')
(我知道如何使用JQuery做到这一点,但是我不能使用jQuery或任何其他类似的库)
(I know how to do it with JQuery but, I can't use jQuery or any other similar library)
答
以纯JavaScript编写:
In plain javascript:
var select = document.getElementById('color');
var currentOpt = select.options[select.selectedIndex];
JsBin示例: http://jsbin.com/ogunet/1/edit (打开您的js控制台)
JsBin example: http://jsbin.com/ogunet/1/edit (open your js console)