demang
08-20-2003, 10:40 PM
Hello friend.. I have a problem here. How can I get multiple values from selected item from a list box? I've tried some ways but it didn't work.. anyway I never write code using javascript.. pls help..
demang
Code example I did but fail..
<html>
<head>
<script language="javascript">
function getSelectedValues (select)
{
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
{
r[r.length] = select.options[i].value;
}
return r;
}
</script>
<script language="javascript">
function getSelectedValues2(list){
var len = list.length;
var ctr=0;
var values = new Array();
for(x=0;x<len;x++){
if(list[x].selected && list[x].value!="-1"){
values[ctr] = list[x].value;
ctr++;
}
}
return values;
}
function testSelect2 (form) {
alert( form.customer_type.options.length );
for (x=0;x<form.customer_type.options.length;x++){
alert(form.customer_type.options[customer_type.selectedIndex.value]);
if (form.customer_type.selectedIndex.selected) {
Item = form.customer_type.selectedIndex[x];
Result = form.customer_type.options[Item].text;
alert (Result);
}
}
}
</script>
</head>
<body>
<form name="myForm">
<select name="customer_type" size="5" multiple="true">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>
<!--<input type="button" name"button1" value="Get Value!" OnClick="myArray=getSelectedValues(this.form)">-->
<input type="button" name"button2" value="Get Value!" onClick="testSelect2(this.form)">
</form>
demang
Code example I did but fail..
<html>
<head>
<script language="javascript">
function getSelectedValues (select)
{
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
{
r[r.length] = select.options[i].value;
}
return r;
}
</script>
<script language="javascript">
function getSelectedValues2(list){
var len = list.length;
var ctr=0;
var values = new Array();
for(x=0;x<len;x++){
if(list[x].selected && list[x].value!="-1"){
values[ctr] = list[x].value;
ctr++;
}
}
return values;
}
function testSelect2 (form) {
alert( form.customer_type.options.length );
for (x=0;x<form.customer_type.options.length;x++){
alert(form.customer_type.options[customer_type.selectedIndex.value]);
if (form.customer_type.selectedIndex.selected) {
Item = form.customer_type.selectedIndex[x];
Result = form.customer_type.options[Item].text;
alert (Result);
}
}
}
</script>
</head>
<body>
<form name="myForm">
<select name="customer_type" size="5" multiple="true">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>
<!--<input type="button" name"button1" value="Get Value!" OnClick="myArray=getSelectedValues(this.form)">-->
<input type="button" name"button2" value="Get Value!" onClick="testSelect2(this.form)">
</form>