Problema con funzione JS

bitox

Nuovo Utente
11 Dic 2012
20
0
0
Salve io ho un problema con un Funzione JS che vada a modificare un elemento select da una checkbox.
ecco il testo della funzione
Codice:
<script type="text/javascript">
<!--
function buildSelect(x) 
{ //alert(x) ; //return
 if (x=='1')
			{
option0 = new Option('1', 0);
option1 = new Option('2', 1);
option2 = new Option('3', 2);
option3 = new Option('4', 3);
option4 = new Option('5', 4);
option5 = new Option('6', 5);
option6 = new Option('7', 6);
option7 = new Option('8', 7);
option8 = new Option('9', 8);
option9 = new Option('10', 9);
option10 = new Option('11', 10);
option11 = new Option('12', 11);
option12 = new Option('13', 12);
option13 = new Option('14', 13);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
document.form1.fieldB.options[10] = option10;
document.form1.fieldB.options[11] = option11;
document.form1.fieldB.options[12] = option12;
document.form1.fieldB.options[13] = option13;
retur;
} if (x=='2')
			{
option0 = new Option('15', 0);
option1 = new Option('16', 1);
option2 = new Option('17', 2);
option3 = new Option('18', 3);
option4 = new Option('19', 4);
option5 = new Option('20', 5);
option6 = new Option('21', 6);
option7 = new Option('22', 7);
option8 = new Option('23', 8);
option9 = new Option('24', 9);
option10 = new Option('25', 10);
option11 = new Option('26', 11);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
document.form1.fieldB.options[10] = option10;
document.form1.fieldB.options[11] = option11;
retur;
} if (x=='3')
			{
option0 = new Option('27', 0);
option1 = new Option('28', 1);
option2 = new Option('29', 2);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
retur;
} if (x=='4')
			{
option0 = new Option('30', 0);
option1 = new Option('31', 1);
option2 = new Option('32', 2);
option3 = new Option('33', 3);
option4 = new Option('34', 4);
option5 = new Option('35', 5);
option6 = new Option('36', 6);
option7 = new Option('37', 7);
option8 = new Option('38', 8);
option9 = new Option('39', 9);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
retur;
} if (x=='5')
			{
retur;
} if (x=='6')
			{
option0 = new Option('40', 0);
option1 = new Option('41', 1);
option2 = new Option('42', 2);
option3 = new Option('43', 3);
option4 = new Option('44', 4);
option5 = new Option('45', 5);
option6 = new Option('46', 6);
option7 = new Option('47', 7);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
retur;
} if (x=='7')
			{
option0 = new Option('48', 0);
option1 = new Option('49', 1);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
retur;
} if (x=='8')
			{
option0 = new Option('50', 0);
option1 = new Option('51', 1);
option2 = new Option('52', 2);
option3 = new Option('53', 3);
option4 = new Option('54', 4);
option5 = new Option('55', 5);
option6 = new Option('56', 6);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
retur;
} if (x=='9')
			{
option0 = new Option('57', 0);
option1 = new Option('58', 1);
option2 = new Option('59', 2);
option3 = new Option('60', 3);
option4 = new Option('61', 4);
option5 = new Option('62', 5);
option6 = new Option('63', 6);
option7 = new Option('64', 7);
option8 = new Option('65', 8);
option9 = new Option('66', 9);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
retur;
} if (x=='10')
			{
option0 = new Option('67', 0);
option1 = new Option('68', 1);
option2 = new Option('69', 2);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
retur;
} if (x=='11')
			{
retur;
} if (x=='12')
			{
option0 = new Option('70', 0);
option1 = new Option('71', 1);
option2 = new Option('72', 2);
option3 = new Option('73', 3);
option4 = new Option('74', 4);
option5 = new Option('75', 5);
option6 = new Option('76', 6);
option7 = new Option('77', 7);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
retur;
} if (x=='13')
			{
option0 = new Option('78', 0);
option1 = new Option('79', 1);
option2 = new Option('80', 2);
option3 = new Option('81', 3);
option4 = new Option('82', 4);
option5 = new Option('83', 5);
option6 = new Option('84', 6);
option7 = new Option('85', 7);
option8 = new Option('86', 8);
option9 = new Option('87', 9);
option10 = new Option('88', 10);
option11 = new Option('89', 11);
option12 = new Option('90', 12);
option13 = new Option('91', 13);
option14 = new Option('92', 14);
option15 = new Option('93', 15);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
document.form1.fieldB.options[10] = option10;
document.form1.fieldB.options[11] = option11;
document.form1.fieldB.options[12] = option12;
document.form1.fieldB.options[13] = option13;
document.form1.fieldB.options[14] = option14;
document.form1.fieldB.options[15] = option15;
retur;
} if (x=='14')
			{
option0 = new Option('94', 0);
option1 = new Option('95', 1);
option2 = new Option('96', 2);
option3 = new Option('97', 3);
option4 = new Option('98', 4);
option5 = new Option('99', 5);
option6 = new Option('100', 6);
option7 = new Option('101', 7);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
retur;
} if (x=='15')
			{
retur;
} if (x=='16')
			{
option0 = new Option('102', 0);
option1 = new Option('103', 1);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
retur;
} if (x=='17')
			{
option0 = new Option('104', 0);
option1 = new Option('105', 1);
option2 = new Option('106', 2);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
retur;
} if (x=='18')
			{
retur;
} if (x=='19')
			{
option0 = new Option('107', 0);
option1 = new Option('108', 1);
option2 = new Option('109', 2);
option3 = new Option('110', 3);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
retur;
} if (x=='20')
			{
retur;
} if (x=='21')
			{
retur;
} if (x=='22')
			{
option0 = new Option('111', 0);
option1 = new Option('112', 1);
option2 = new Option('113', 2);
option3 = new Option('114', 3);
option4 = new Option('115', 4);
option5 = new Option('116', 5);
option6 = new Option('117', 6);
option7 = new Option('118', 7);
option8 = new Option('119', 8);
option9 = new Option('120', 9);
option10 = new Option('121', 10);
option11 = new Option('122', 11);
option12 = new Option('123', 12);
option13 = new Option('124', 13);
option14 = new Option('125', 14);
option15 = new Option('126', 15);
option16 = new Option('127', 16);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
document.form1.fieldB.options[10] = option10;
document.form1.fieldB.options[11] = option11;
document.form1.fieldB.options[12] = option12;
document.form1.fieldB.options[13] = option13;
document.form1.fieldB.options[14] = option14;
document.form1.fieldB.options[15] = option15;
document.form1.fieldB.options[16] = option16;
retur;
} if (x=='23')
			{
option0 = new Option('128', 0);
option1 = new Option('129', 1);
option2 = new Option('130', 2);
option3 = new Option('131', 3);
option4 = new Option('132', 4);
option5 = new Option('133', 5);
option6 = new Option('134', 6);
option7 = new Option('135', 7);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
retur;
} if (x=='24')
			{
option0 = new Option('136', 0);
option1 = new Option('137', 1);
option2 = new Option('138', 2);
option3 = new Option('139', 3);
option4 = new Option('140', 4);
option5 = new Option('141', 5);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
retur;
} if (x=='25')
			{
option0 = new Option('142', 0);
option1 = new Option('143', 1);
option2 = new Option('144', 2);
option3 = new Option('145', 3);
option4 = new Option('146', 4);
option5 = new Option('147', 5);
option6 = new Option('148', 6);
option7 = new Option('149', 7);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
retur;
} if (x=='26')
			{
option0 = new Option('150', 0);
option1 = new Option('151', 1);
option2 = new Option('152', 2);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
retur;
} if (x=='27')
			{
option0 = new Option('153', 0);
option1 = new Option('154', 1);
option2 = new Option('155', 2);
option3 = new Option('156', 3);
option4 = new Option('157', 4);
option5 = new Option('158', 5);
option6 = new Option('159', 6);
option7 = new Option('160', 7);
option8 = new Option('161', 8);
option9 = new Option('162', 9);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
document.form1.fieldB.options[6] = option6;
document.form1.fieldB.options[7] = option7;
document.form1.fieldB.options[8] = option8;
document.form1.fieldB.options[9] = option9;
retur;
} if (x=='28')
			{
option0 = new Option('163', 0);
option1 = new Option('164', 1);
option2 = new Option('165', 2);
option3 = new Option('166', 3);
option4 = new Option('167', 4);
option5 = new Option('168', 5);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
retur;
} if (x=='29')
			{
option0 = new Option('169', 0);
option1 = new Option('170', 1);
option2 = new Option('171', 2);
option3 = new Option('172', 3);
option4 = new Option('173', 4);
option5 = new Option('174', 5);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
document.form1.fieldB.options[4] = option4;
document.form1.fieldB.options[5] = option5;
retur;
} if (x=='30')
			{
retur;
} if (x=='31')
			{
retur;
} if (x=='32')
			{
retur;
} if (x=='33')
			{
option0 = new Option('175', 0);
option1 = new Option('176', 1);
option2 = new Option('177', 2);
option3 = new Option('178', 3);
document.form1.fieldB.options[0] = option0;
document.form1.fieldB.options[1] = option1;
document.form1.fieldB.options[2] = option2;
document.form1.fieldB.options[3] = option3;
retur;
}
}
// -->
</script>

La funzione è molto basilare direi, ma quando vado a cambiare la selezione delle checkbox nel select appare anche una parte dei dati che mi portavo dietro dalla selezione precedente, qualcuno può aiutarmi?
 

Fez Vrasta

Utente Attivo
11 Mag 2013
409
0
0
Ciao, potresti spiegare più precisamente, magari con un esempio, cosa dovrebbe fare questo codice?

Ho come l'impressione che ti stia complicando immensamente la vita.
 

bitox

Nuovo Utente
11 Dic 2012
20
0
0
Certo, anzi mi scuso per la poca chiarezza.
io ho un elenco di RadioBotton e un elemento select.
quando premo su una qualsiasi di queste radio il campo select si dovrebbe popolare di diversi elementi in base alla selezione.
Ora la prima volta che faccio una selezione funziona tutto alla perfezione ma se vado a seleziona un radiobotton diverso lo script mi mette il contenuto esatto più quello che avrebbe dovuto mettere con la selezione precedente.
esempio.

due radio "1" e "2", quando clicco su "1" nella select appare "3,4" e se clicco "2" appare "5,6". Nel mio caso appare se clicco prima "1" poi "2" appare "5,6,3,4".
 
Ultima modifica:

Fez Vrasta

Utente Attivo
11 Mag 2013
409
0
0
comunque se sono statici farei qualcosa tipo:

Codice:
$(function() { 

  $(".radio").change( function() {
  
    value = $(this).val();

    switch(value) {

      case 1:
        $("#selectbox").html("<select> ... </select>");
        break;

     case 2:
        $("#selectbox").html("<select> ... </select>");
        break;

     }

  });

});

Sostituisci <select> ... </select> con le tue liste, ed in teoria dovrebbe funzionare tutto.

Logicamente devi includere jquery
 
Discussioni simili
Autore Titolo Forum Risposte Data
M Problema con connessione MySqli e funzione PHP 2
A [PHP] Problema invio mail con funzione mail() PHP 3
F [Javascript] Problema funzione jquery con elementi css esterni Javascript 1
S Problema con funzione "imagecreatefrompng" PHP 10
R Problema funzione caricate con il body Javascript 1
G Problema con la funzione array_combine PHP 2
L problema con la funzione header!!! AIUTO! PHP 24
M problema con elementi della funzione Javascript 3
L problema nella implementare una funzione con ritorno PHP 2
F Problema con funzione cambia password PHP 5
S Problema return con funzione PHP 2
L problema email con funzione mail PHP 1
S problema con funzione php PHP 3
D Problema apostrofo con funzione unlink PHP 7
C Problema nel Caricare file e spostarlo con la funzione move_uploaded_file PHP 14
D Problema con funzione preg_match() per controllo email PHP 2
Neptune7650 Piccolo problema con la funzione include PHP 1
asevenx problema con funzione per far apparire e scomparire un form di commenti PHP 1
asevenx problema con la funzione AVG() PHP 1
P Problema con la funzione mail PHP 13
G ajax php myqsl - problema con funzione javascript Ajax 1
F Problema con funzione di RainTpl PHP 3
dk-wamp problema con la funzione sleep PHP 4
jan267 Problema con funzione getElementsByClassName Javascript 2
O Principiante: problema con funzione CURL ###URGENTE### PHP 1
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
N Problema con position absolute e overflow HTML e CSS 4
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
K [PHP] Problema con variabili concatenate. PHP 1
O problema con query PHP 4
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
M Problema con Try Catch PHP 0
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
R problema con else PHP 0
T PROBLEMA CON ARRAY PHP 8
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4

Discussioni simili