<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
</head>
<body>
<script language="JavaScript">
<!--
function checkselect(objname){
o = document.getElementById(objname);
t = document.getElementById("output");
var intvalue="";
for(i=0;i<o.length;i++){
if(o.options[i].selected){
intvalue+=o.options[i].value+",";
}
}
t.value=intvalue.substr(0,intvalue.length-1);
}
//-->
</script>
<select name="objsel" size=8 multiple>
<option value="0" selected>请选择...
<option value="1">测试一
<option value="2">测试二
<option value="3">测试三
<option value="4">测试四
<option value="5">测试五
</select>
<input type="button" οnclick="checkselect('objsel');" value="输出">
选中的项目:<input type="text" name="output">
</body>
</html>
转:http://zhoujingxian.iteye.com/blog/559804
/**
* 获取multiple Select的值
* @param obj multiple select元素
* @returns {Array}
*/
functiongetMultiple(obj){
var
arr = [];
var
options = obj.options;
while
(obj.selectedIndex != -
1
){
arr.push(options[obj.selectedIndex].value);
options[obj.selectedIndex].selected =
false
;
}
return
arr;
}
通过对选中项逐一取消选择,得到选中项的值的数组。这个方法的妙处就在避免了在选项非常多时遍历所有选项(这个遍历对旧版本的浏览器来说会很慢)
用法很简单:
var
arr = getMultiple(document.getElementById(
'select_m'
));
\\
do
something
with
arr ...
本文介绍了一个简单的JavaScript方法,用于从<select>元素中获取多个已选中的选项值,并通过避免遍历所有选项来提高效率。适用于前端开发中的表单处理。
4911

被折叠的 条评论
为什么被折叠?



