Foros del Web » Programando para Internet » Javascript »

problemas con mi funcion javascript

Estas en el tema de problemas con mi funcion javascript en el foro de Javascript en Foros del Web. var stat_string = ''; function select_status(val){ var mul = document.getElementById("multiple"); if(mul.value.indexOf(val)==-1){ mul.value+=(mul.value!=''?',':'')+val; }else{ if(mul.value.indexOf(val)!=-1){ mul.value=mul.value.replace(val,''); mul.value=mul.value.replace(',',''); } } <select multiple name="stat1[]" id="stat1" onChange="select_status(this.options[this.selectedIndex].value)"> <option value="A">A ...
  #1 (permalink)  
Antiguo 07/05/2007, 11:16
Avatar de germana  
Fecha de Ingreso: abril-2007
Mensajes: 61
Antigüedad: 17 años
Puntos: 0
problemas con mi funcion javascript

var stat_string = '';
function select_status(val){
var mul = document.getElementById("multiple");
if(mul.value.indexOf(val)==-1){
mul.value+=(mul.value!=''?',':'')+val;
}else{
if(mul.value.indexOf(val)!=-1){
mul.value=mul.value.replace(val,'');
mul.value=mul.value.replace(',','');
}
}


<select multiple name="stat1[]" id="stat1" onChange="select_status(this.options[this.selectedIndex].value)">
<option value="A">A - Active Avaliable </option>
<option value="P">P - Foreclosure</option>
<option value="CS">CS - Closed Sale</option>
<option value="PS">PS - Pending Sale</option></select>

<input type="text" name="multiple" id="multiple" onMouseover="dropdownmenu(this, event, menu1, '150px')" onMouseout="delayhidemenu()" />

--------------------------------------------------------
ok... este es mi codigo, y el problema es que si selecciono PS y luego P, en vez de colocar en el input PS,P lo que hace es borrarme la P del PS quedandome S,P......

Lo que quisiera saber es si existe otra forma MEJOR de borrar los string del input o alguna forma de corregir eso....

Gracias
  #2 (permalink)  
Antiguo 07/05/2007, 13:20
Avatar de derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: problemas con mi funcion javascript

Lo malo es que al hacer
mul.value.indexOf(val)
tienes un valor que es "PS" y otro que es "P", luego si está "PS" escrito en el campo, mul.value.indexOf("P") sí va a dar != -1, luego algo funciona mal.


¿Cual es tu propósito exactamente? ¿Quieres agregar "P" o "PS" al input según se seleccionen, pero siempre que no existan en el campo?
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.
  #3 (permalink)  
Antiguo 07/05/2007, 13:27
Avatar de chalchis  
Fecha de Ingreso: julio-2003
Mensajes: 1.773
Antigüedad: 20 años, 9 meses
Puntos: 21
Re: problemas con mi funcion javascript

prueba asi
<select size="4" name="dirigido[]" id="dirigido" multiple onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);" class="selectx">
<option value="2" selected>Alumno</option>

<option value="4">Catedratico</option>
<option value="8">Administrativo</option>
<option value="16">Directivo</option>
<option value="1">Invitado</option>
<option value="v">Visitante</option>
</select>


<script>
var arrOldValues;

function SelectAllList(CONTROL){
for(var i = 0;i < CONTROL.length;i++){
CONTROL.options[i].selected = true;
}
}

function DeselectAllList(CONTROL){
for(var i = 0;i < CONTROL.length;i++){
CONTROL.options[i].selected = false;
}
}


function FillListValues(CONTROL){
var arrNewValues;
var intNewPos;
var strTemp = GetSelectValues(CONTROL);
arrNewValues = strTemp.split(",");
for(var i=0;i<arrNewValues.length-1;i++){
if(arrNewValues[i]==1){
intNewPos = i;
}
}

for(var i=0;i<arrOldValues.length-1;i++){
if(arrOldValues[i]==1 && i != intNewPos){
CONTROL.options[i].selected= true;
}
else if(arrOldValues[i]==0 && i != intNewPos){
CONTROL.options[i].selected = false;
}

if(arrOldValues[intNewPos]== 1){
CONTROL.options[intNewPos].selected = false;
}
else{
CONTROL.options[intNewPos].selected = true;
}
}
}


function GetSelectValues(CONTROL){
var strTemp = "";
for(var i = 0;i < CONTROL.length;i++){
if(CONTROL.options[i].selected == true){
strTemp += "1,";
}
else{
strTemp += "0,";
}
}
return strTemp;
}

function GetCurrentListValues(CONTROL){
var strValues = "";
strValues = GetSelectValues(CONTROL);
arrOldValues = strValues.split(",")
}
</script>
__________________
gerardo
  #4 (permalink)  
Antiguo 14/05/2007, 15:05
Avatar de germana  
Fecha de Ingreso: abril-2007
Mensajes: 61
Antigüedad: 17 años
Puntos: 0
Re: problemas con mi funcion javascript

Eso esta muy bien......... pero como hago para agregar esa funcionalidad a mi codigo????

es decir, la idea es que tengo un metodo javascript que hace que un select se muestre como menu desplegable cuando onmouseover en un input llamado "multiple" ahora, cuando selecciono algo del menu desplegable (que en realidad es un select) la opcion que elijo debe mostrarse en el input y ademas marcarse com seleccionada... y cuando la deselecciono del select deberia borrarse del input..... hasta ahora lo unico que hace mi codigo es mostrar las selecciones y borrarlas pero no las selecciona.. es decir no las marca de azul en el select

Gracias.. por su tiempo....
  #5 (permalink)  
Antiguo 15/05/2007, 10:46
Avatar de germana  
Fecha de Ingreso: abril-2007
Mensajes: 61
Antigüedad: 17 años
Puntos: 0
Re: problemas con mi funcion javascript

Cita:
Iniciado por derkenuke Ver Mensaje
Lo malo es que al hacer
mul.value.indexOf(val)
tienes un valor que es "PS" y otro que es "P", luego si está "PS" escrito en el campo, mul.value.indexOf("P") sí va a dar != -1, luego algo funciona mal.


¿Cual es tu propósito exactamente? ¿Quieres agregar "P" o "PS" al input según se seleccionen, pero siempre que no existan en el campo?
----------------------------------------
Si, quiero agregar las selecciones al input segun se seleccionan y si ya existen y se vuelven a seleccionar borrar la seleccion... ademas quisiera que cuando se copien queden seleccionadas, es decir resultadas en azul (como cuando se utiliza la tecla CONTROL)
  #6 (permalink)  
Antiguo 16/05/2007, 16:29
Avatar de derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: problemas con mi funcion javascript

¿Cómo? ¿Algo así?

Código PHP:
<select id="sel" onChange="cambioSel()" size="4">
    <
option value="A">Active Avaliable </option>
    <
option value="P">Foreclosure</option>
    <
option value="CS">CS Closed Sale</option>
    <
option value="PS">PS Pending Sale</option>
</
select>
<
br />
<
input type="text" id="caja" />


<
script>

function 
estaSeleccionado(elemOption) {    return elemOption.getAttribute("seleccionado")=="si"; }
function 
setSeleccionado(elemOptionvalor) { elemOption.setAttribute("seleccionado",valor); }

function 
cambioSel() {
    
//elemento al que se ha cambiado
    
var opcion=document.getElementById("sel").optionsdocument.getElementById("sel").options.selectedIndex ];
    var 
valorestaSeleccionado(opcion) ? "no" "si";
    
setSeleccionadoopcion valor);
    
actualizaCaja();
}

function 
actualizaCaja() {
    var 
opciones=document.getElementById("sel").options;
    var 
caja=document.getElementById("caja");
    for(var 
i=0seleccionados=new Array(); i<opciones.lengthi++) 
        if( 
estaSeleccionado(opciones[i]) )
            
seleccionados[seleccionados.length] = opciones[i].value;
    
caja.value=seleccionados.join(", ");
}


</script> 
Especifica qué quieres hacer en cada evento, qué se tiene que añadir a la caja y por qué, temo no entender tu propósito.

¿Quieres hacer algo como eso o quieres que en la caja se muestren sólo los que están seleccionados? Esto sería mucho más sencillo.


Un saludo.
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.
  #7 (permalink)  
Antiguo 17/05/2007, 08:03
Avatar de germana  
Fecha de Ingreso: abril-2007
Mensajes: 61
Antigüedad: 17 años
Puntos: 0
Re: problemas con mi funcion javascript

Cita:
Iniciado por derkenuke Ver Mensaje
¿Cómo? ¿Algo así?

Código PHP:
<select id="sel" onChange="cambioSel()" size="4">
    <
option value="A">Active Avaliable </option>
    <
option value="P">Foreclosure</option>
    <
option value="CS">CS Closed Sale</option>
    <
option value="PS">PS Pending Sale</option>
</
select>
<
br />
<
input type="text" id="caja" />


<
script>

function 
estaSeleccionado(elemOption) {    return elemOption.getAttribute("seleccionado")=="si"; }
function 
setSeleccionado(elemOptionvalor) { elemOption.setAttribute("seleccionado",valor); }

function 
cambioSel() {
    
//elemento al que se ha cambiado
    
var opcion=document.getElementById("sel").optionsdocument.getElementById("sel").options.selectedIndex ];
    var 
valorestaSeleccionado(opcion) ? "no" "si";
    
setSeleccionadoopcion valor);
    
actualizaCaja();
}

function 
actualizaCaja() {
    var 
opciones=document.getElementById("sel").options;
    var 
caja=document.getElementById("caja");
    for(var 
i=0seleccionados=new Array(); i<opciones.lengthi++) 
        if( 
estaSeleccionado(opciones[i]) )
            
seleccionados[seleccionados.length] = opciones[i].value;
    
caja.value=seleccionados.join(", ");
}


</script> 
Especifica qué quieres hacer en cada evento, qué se tiene que añadir a la caja y por qué, temo no entender tu propósito.

¿Quieres hacer algo como eso o quieres que en la caja se muestren sólo los que están seleccionados? Esto sería mucho más sencillo.


Un saludo.
Lo que necesito es esto:

si en el combo selecciono A.. en el input debe aparecer A y ademas dejar seleccionada en el combo A... si luego selecciono PS el input debe mostrar A,PS y en el combo dejar seleccionadas A y PS... etc
  #8 (permalink)  
Antiguo 17/05/2007, 13:01
Avatar de derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: problemas con mi funcion javascript

Bueno, la primera parte ya la tendríamos (en FF, en IE no se lo que puede pasar, no me ha funcionado ni guardando los valores en un select, debe ser la manera de seleccionar option).

Para la segunda parte es fácil, recorremos todas las option del select, y las que están seleccionadas las agregamos y las que no, pues no.

Un saludo.
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.
  #9 (permalink)  
Antiguo 17/05/2007, 18:30
Avatar de programeitor  
Fecha de Ingreso: febrero-2005
Mensajes: 994
Antigüedad: 19 años, 2 meses
Puntos: 9
Re: problemas con mi funcion javascript

Hola, para que quede seleccionado en el combo dale option.selected=true
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:50.