Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/09/2010, 11:16
tribano
 
Fecha de Ingreso: diciembre-2009
Mensajes: 51
Antigüedad: 14 años, 5 meses
Puntos: 1
problema al recoger datos de un select multiple modificado con javascript

Buenas

Primero he decir que no se, si mi duda iria aqui o en el foro de javascript, si es asi ruego que me lo digan.

tengo un select multiple que se va rellenando desde un select, esto funciona en java.

Código HTML:
<html>
<head>
<title>Selecciona elementos en lista 2</title>

</head>

<BODY style="font-family: Verdana">
<p align="center"><b>Selecciona elementos en lista 2</b></p>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Kathi O'Shea (Kathi.O'[email protected]) -->
<!-- Web Site:  http://www.web-savant.com/users/kathi/asp -->


<!-- Begin
function moveOver()  
{
var boxLength = document.choiceForm.choiceBox.length;
var selectedItem = document.choiceForm.available.selectedIndex;
var selectedText = document.choiceForm.available.options[selectedItem].text;
var selectedValue = document.choiceForm.available.options[selectedItem].value;
var i;
var isNew = true;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
thisitem = document.choiceForm.choiceBox.options[i].text;
if (thisitem == selectedText) {
isNew = false;
break;
      }
   }
} 
if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
document.choiceForm.choiceBox.options[boxLength] = newoption;
}
document.choiceForm.available.selectedIndex=-1;
}
function removeMe() {
var boxLength = document.choiceForm.choiceBox.length;
arrSelected = new Array();
var count = 0;
for (i = 0; i < boxLength; i++) {
if (document.choiceForm.choiceBox.options[i].selected) {
arrSelected[count] = document.choiceForm.choiceBox.options[i].value;
}
count++;
}
var x;
for (i = 0; i < boxLength; i++) {
for (x = 0; x < arrSelected.length; x++) {
if (document.choiceForm.choiceBox.options[i].value == arrSelected[x]) {
document.choiceForm.choiceBox.options[i] = null;
   }
}
boxLength = document.choiceForm.choiceBox.length;
   }
}
function saveMe() {
var strValues = "";
var boxLength = document.choiceForm.choiceBox.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.choiceBox.options[i].value;
}
else {
strValues = strValues + "," + document.choiceForm.choiceBox.options[i].value;
}
count++;
   }
}
if (strValues.length == 0) {
alert("No has hecho ninguna selección");
}
else {
alert("Aquí están los valores seleccionados:\r\n" + strValues);
   }
}
//  End -->
</script>
</HEAD>

<html>
<head>

<title>Selección de elementos</title>

</head>


<BODY style="font-family: Verdana">

<center>
<form name="choiceForm">
<table border=0 width="414">
<tr>
<td valign="top" width=227>
<font size="2">Contenido disponible: </font>
<br>
<select name="available" size=10 onchange="moveOver();">

<option value=1>Noticias de la compañía
<option value=2>Noticias de industria
<option value=3>Actualizaciones de productos
<option value=4>Especificaciones de productos
<option value=5>Historia de pedidos
<option value=6>Estado del pedido
<option value=7>Contactos
<option value=8>Calendario de eventos
<option value=9>Planificación
<option value=10>Notas
</select>
</td>
<td valign="top" width="177">
<font size="2">Tu selección: </font>
<br>
<select multiple name="choiceBox" style="width:150;" size="10">

</select>
</td>
</tr>
<tr>
<td colspan=2 height=10 width="390">
<input type="button" value="Eliminar seleccionados" onclick="removeMe();" style="font-size: 8pt">
<input type="button" value="Obtener valores seleccionados" onclick="saveMe();" style="font-size: 8pt">
</td>
</tr>
</table>
</form>
</center>

</body>
</html> 
Esto lo he cogido de aqui:
http://javascripts.astalaweb.com/For...larios%20V.asp

Es la opción selecciona elementos de la lista2 (por si alguien quiere verlo en funcionamiento).

He estado mirando el foro y he encontrado varias posibles soluciones, como por ejemplo:

Código HTML:
<script language="javascript">
	function selectAll()
	{
		for ( i = 0; i < document.forma.test.options.length; i++ )
			document.forma.test.options[i].selected = true;
	}
</script>
<form name="forma">
	<select name="test" multiple>
		<option value="1">1</option>
		<option value="1">2</option>
		<option value="1">3</option>
		<option value="1">4</option>
		<option value="1">5</option>				
	</select>
	<br />
	<input type="button" value="enviar" onclick="selectAll();" />
</form> 
Con esta función seleccionaria todos los valores del choiceBox.
Fuente http://www.forosdelweb.com/f15/recog...select-392570/

y para recogerlos del form, mediante el post, podria hacer esto:

Código PHP:
<select name="choicebox[]" multiple size="3">  

<?
foreach ($_POST['choiceBox'] as $indice => $valor){
echo 
"indice: ".$indice." => ".$valor."<br>";
}
?>
Fuente:http://www.forosdelweb.com/f18/selec...alores-196709/

y aqui es donde tengo el problema, cuando intento convertir el choiceBox en una array "[]" Deja de funcionarme la función java moveOver().

Gracias.