したがって、複数選択とも呼ばれる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>