因此,如果您有一个 html 列表框,也称为多选,并且您想生成一个逗号分隔的字符串来列出该列表框中的所有值,您可以使用以下示例来做到这一点。list_to_string() js 函数是这里唯一重要的东西。您可以在http://josh.gourneau.com/sandbox/js/list_to_string.html使用此页面
<html>
<head>
<script>
function list_to_string()
{
var list = document.getElementById('list');
var chkBox = document.getElementById('chk');
var str = document.getElementById('str');
textstring = "";
for(var i = 0; i < list.options.length; ++i){
comma = ",";
if (i == (list.options.length)-1){
comma = "";
}
textstring = textstring + list[i].value + comma;
str.value = textstring;
}
}
</script>
</head>
<body>
<form>
<select name="list" id="list" size="3" multiple="multiple">
<option value="India">India</option>
<option value="US">US</option>
<option value="Germany">Germany</option>
</select>
<input type="text" id="str" name="str" />
<br /><br />
<input type="button" id="chk" value="make a string!" name="chk" onclick="list_to_string();"/>
</form>
</body>
</html>