|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Oct 2009
Messaggi: 1
|
[JAVASCRIPT] Ciclo su options select
Chiedo aiuto per questo problema: ho due select create dinamicamente. Quando seleziono il valore dalla selectA i valori nella selectB cambiano (il tutto con ajax e php).
Se seleziono con il mouse il valore del selectA i valori di selectB cambiano correttamente. A volte ho bisogno di far fare questa operazione in automatico, senza che la faccia l'utente. Ho creato uno script js che funziona in parte. Frammento di codice modifybar.js: n ed m sono rispettivamente selectA e selectB Codice:
var n=document.getElementById("user_category");
var m=document.getElementById("type_barrier");
var y=0;
var found=false;
while (!found){
y=y+1;
n.selectedIndex=y;
loadList('type_barrier',getSelected(n));
alert("ciao");
for (x=1; x<m.length; x++){
m.selectedIndex=x;
if (m.value==vc){
found=true;
break;
}
}
Codice:
var xmlHttp = getXmlHttpObject();
function loadList(tb, id){
xmlHttp.open('GET', 'request.php?table='+tb+'&id='+id, true);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.send(null);
}
function addOption(select, value, text) {
var option = document.createElement("option");
option.value = value+'§'+text,
option.text = text;
try {
select.add(option, null);
} catch(e) {
// For Internet Explorer
select.add(option);
}
}
function getSelected(select) {
// Return <option> value selected
return select.options[select.selectedIndex].value;
}
function stateChanged() {
if(xmlHttp.readyState == 4) {
// State OK
if (xmlHttp.status == 200) {
var resp = xmlHttp.responseText;
if(resp) {
// Couple of value into answer string are separated from ;
var values = resp.split(';');
// First element is list ID
var listId = values.shift();
var select = document.getElementById(listId);
select.disabled=false;
// Delete old values
while (select.options.length) {
select.remove(0);
}
if(listId == 'user_category') {
addOption (select, 0, '-- Select user --');
}
else {
addOption (select, 0, '-- Select barrier --');
}
var limit = values.length;
for(i=0; i < limit; i++) {
var pair = values[i].split('|');
addOption(select, pair[0], pair[1]);
}
}
} else {
alert(xmlHttp.responseText);
}
}
}
function getXmlHttpObject(){
var xmlHttp=null;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
PROBLEMA1: se da modifybar.js lascio alert("ciao") lo script funziona. Siccome l'alert non lo vorrei (era stato messo in fase di debug per verificarne il comportamento) l'ho tolto ma lo script mi cicla su tutti i valori delle due select e per ogni voce di selectA mi lancia due alert vuoti. PROBLEMA2: loadList ho dovuto richiamarla a mano perche' con il ciclo di modifybar.js l'evento onchange nella selectA che richiama loadList non va. Attendo con fiducia! Un saluto da un nuovo utente. Ultima modifica di scarec : 29-10-2009 alle 16:57. Motivo: non avevo indentato bene il codice, scusate |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:12.



















