Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 获得一些选定的选项javascript [重复](get a few selected option javascript [duplicate])

How do I get the selected value from a dropdown list in case there are more than one selected item using JavaScript?

(万一使用JavaScript有多个选定项,如何从下拉列表中获得选定值?)

like this:

(像这样:)

<select name"nana" id="nana">
  <option value="0"</option>
  <option value="1"</option>
  <option selected="selected" value="2"</option>
  <option value="3"</option>
  <option selected="selected" value="4"</option>
</select>

I tried:

(我试过了:)

var e = document.getElementById("nana");
var strUser = e.options[e.selectedIndex].value;
document.querySelector("nana").value;

and its return only the first selected value (2), how can i get all of the selected items?

(并且它仅返回第一个选定的值(2),如何获得所有选定的项?)

  ask by EladMag translate from so

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

1 Reply

0 votes
by (71.8m points)

add multiple="multiple" for multiple for more than 1 select

(为多个选择多个添加“多个”)

 var selectNana = document.getElementById("nana"); var selectedNana = []; for (var i = 0; i < selectNana.length; i++) { if (selectNana.options[i].selected) selectedNana.push(selectNana.options[i].value); } console.log(selectedNana); 
 <select name"nana" id="nana" multiple="multiple" > <option value="0">0</option> <option value="1">1</option> <option selected="selected" value="2">2</option> <option value="3">3</option> <option selected="selected" value="4">4</option> </select> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...