Problema con funzione JS

  • Creatore Discussione Creatore Discussione bitox
  • Data di inizio Data di inizio

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?
 
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.
 
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:
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