如何使用javascript从列表框中获取多个选定的值和项目 [英] how to get multiple selected values and items from listbox using javascript
本文介绍了如何使用javascript从列表框中获取多个选定的值和项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!DOCTYPE html>
< html>
< script>
函数getValue()
{
var x = document.getElementById(sel);
for(var i = 0; i< x.options.length; i ++){
if(x.options [i] .selected == true){
alert(x。项[I] .selected);
}
}
}
< / script>
< / head>
< body>
< select multiple =multipleid =sel>
< option value =volvo>沃尔沃< / option>
< option value =saab> Saab< / option>
< option value =opel> Opel< / option>
< option value =audi> Audi< / option>
< / select>
< input type =buttonvalue =Get Valueonclick =getValue()/>
< / body>
< / html>
这是我的代码。如何从列表框中使用javascript获取所有选定的值。上面的代码显示了所有选定值的真实值。 替换替换替换
if(x.options [i] .selected == true){
alert(x.options [i ] .selected);
with
<$ p (x.options [i] .selected){
alert(x.options [i] .value); $ p> if
}
<!DOCTYPE html>
<html>
<script>
function getValue()
{
var x=document.getElementById("sel");
for (var i = 0; i < x.options.length; i++) {
if(x.options[i].selected ==true){
alert(x.options[i].selected);
}
}
}
</script>
</head>
<body>
<select multiple="multiple" id="sel">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="button" value="Get Value" onclick="getValue()"/>
</body>
</html>
This is my code. how do i get all the selected values from listbox using javascript.The above code showing show true for all selected value.
解决方案
Replace
if(x.options[i].selected ==true){
alert(x.options[i].selected);
}
with
if(x.options[i].selected){
alert(x.options[i].value);
}
这篇关于如何使用javascript从列表框中获取多个选定的值和项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文