Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

javascript - 寻找哪个 <Option>在中选择<select> (没有JQuery)(Finding which <Option> is selected in <select> (without JQuery))

I have the following Element:(我有以下元素:)

<select id="color" name="colorId" class="btn secondary"> <option value="347366" selected="selected">Purple</option> <option value="56634">White</option> </select> And I want to find which option is selected:(我想找到选择的选项:) The following give me only the default:(以下仅给我默认值:) document.querySelector('#color option[selected="selected"]') (I know how to do it with JQuery but, I can't use jQuery or any other similar library)((我知道如何使用JQuery做到这一点,但是我不能使用jQuery或任何其他类似的库))   ask by Guy Korland translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the :checked selector.(使用:checked选择器。)

This applies to checkbox, radio, and select(这适用于复选框,单选框和选择) document.querySelector('#color option:checked') for the node(对于节点) document.querySelector('#color option:checked').value for the value(为了价值)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...