Visualizzazione dei risultati da 1 a 10 su 10

Discussione: Array

  1. #1

    Array

    Ciao a tutti,
    il mio problema è il seguente:
    Ecco il mio array
    ---------------------
    function _valori(){
    this.user=""
    this.pwd=""
    this.stato=""
    }

    var dati = new Array()
    dati[0] = new _valori()
    dati[0].user = "fra"
    dati[0].pwd = "fra"
    dati[0].stato = "libero"
    dati[1] = new _valori()
    dati[1].user = "rossano"
    dati[1].pwd = "rossano"
    dati[1].stato = "libero"
    dati[2] = new _valori()
    dati[2].user = "angela"
    dati[2].pwd = "angela"
    dati[2].stato = "libero"
    dati[3] = new _valori()
    dati[3].user = "max"
    dati[3].pwd = "max"
    dati[3].stato = "libero"
    --------------------------------
    Dovrei creare un ciclo che mi tiri fuori il valore di stato, quando è libero devo scrivere al posto di libero occupato ed estrapolare i dati user e pwd relativi a quello stato.

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2003
    Messaggi
    709
    Prova così:
    codice:
    for (var i=0; i<dati.length; i++) {
      if (dati[i].stato=="libero") {
        dati[i].stato="occupato";
        var theUser=dati[i].user;
        var thePwd=dati[i].pwd;
        window.alert("utente "+theUser+" password "+thePwd+": modificato stato da libero a occupato");
      }
    }

  3. #3
    Grazie mille,
    però quello che non riesco capire è possibile proprio scrivere nell'array al posto di libero occupato e come faccio a farlo fermare dove trova il primo libero?

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2003
    Messaggi
    709
    Si, la funzione scrive "occupato" al posto di "libero" e se vuoi che si interrompa dopo aver trovato il primo record in cui era scritto "libero", devi modificarla in questo modo:
    codice:
    for (var i=0; i<dati.length; i++) {
      if (dati[i].stato=="libero") {
        dati[i].stato="occupato";
        var theUser=dati[i].user;
        var thePwd=dati[i].pwd;
        window.alert("utente "+theUser+" password "+thePwd+": modificato stato da libero a occupato");
        break;
      }
    }

  5. #5
    Perfetto.
    Ho ancora un quesito come faccio a passare i dati theUser ThePwd in un form?
    Grazie

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2003
    Messaggi
    709
    codice:
    for (var i=0; i<dati.length; i++) {
      if (dati[i].stato=="libero") {
        dati[i].stato="occupato";
        var theUser=dati[i].user;
        var thePwd=dati[i].pwd;
        
        document.forms['nomeForm'].elements['nomeCampoUser'].value=theUser;
        document.forms['nomeForm'].elements['nomeCampoPwd'].value=thePwd;
        
        window.alert("utente "+theUser+" password "+thePwd+": modificato stato da libero a occupato");
        break;
      }
    }

  7. #7
    Non funziona con elements
    allego tutto il codice della pagina

    code
    -------------------------
    <script language="JavaScript">
    <!--
    function Timer() {
    alert ("Il tempo a tua disposizione per muovere la webcam è terminato");
    }

    function Connect()
    {
    CominsCamX.SetCamData(form1.txtCamName.value, form1.txtIP.value, form1.txtPort.value,
    form1.txtID.value, form1.txtPass.value);
    CominsCamX.ConnectVideo();
    setTimeout('Timer()',30000)
    }

    // -->

    function _valori(){
    this.user=""
    this.pwd=""
    this.stato=""
    }

    var dati = new Array()
    dati[0] = new _valori()
    dati[0].user = "fra"
    dati[0].pwd = "fra"
    dati[0].stato = "libero"
    dati[1] = new _valori()
    dati[1].user = "rossano"
    dati[1].pwd = "rossano"
    dati[1].stato = "libero"
    dati[2] = new _valori()
    dati[2].user = "angela"
    dati[2].pwd = "angela"
    dati[2].stato = "libero"
    dati[3] = new _valori()
    dati[3].user = "max"
    dati[3].pwd = "max"
    dati[3].stato = "libero"

    for (var i=0; i<dati.length; i++) {
    if (dati[i].stato=="libero") {
    dati[i].stato="occupato";
    var theUser=dati[i].user;
    var thePwd=dati[i].pwd;
    form1.txtID.value = theUser;
    document.forms['form1'].elements['txtID'].value=theUser;
    document.forms['form1'].elements['txtPass'].value=thePwd;
    window.alert("utente "+theUser+" password "+thePwd+": modificato stato da libero a occupato");
    break;
    }
    }


    </script>
    <%
    Response.Write (theUser)
    'CONNESSIONE ALLA WEBCAM
    S = S & "<BODY onload=Connect()>"
    S = S & "<p align=center>OPZIONI</font></p>"
    S = S & "<TABLE cellSpacing=1 cellPadding=1 width=100% border=0>"
    S = S & "<TR>"
    S = S & "<TD>"
    'OBJECT WEBCAM
    S = S & "<TABLE id=TABLE1 style=WIDTH: 400px; HEIGHT: 400px height=250 cellSpacing=1 cellPadding=1 width=200 align=center border=0>"
    S = S & "<TR>"
    S = S & "<TD>"
    S = S & "<OBJECT ID='CominsCamX' width=320 height=240 CLASSID='CLSID:A1D73A93-3CFA-4047-8256-9B704AEF0F74' codebase='CominsCamX.cab#Version=1,0,1,3'>"
    S = S & "</OBJECT>"
    S = S & "<form name=form1>"
    S = S & "<input type=text value=WebCam View1 name=txtCamName size=8>"
    S = S & "<input type=text value=10.0.0.239 name=txtIP size=10>"
    S = S & "<input type=text value=700 name=txtPort size=3>"
    S = S & "<input type=text value='aaa' name=txtID size=4>"
    S = S & "<input type=text value='aaa' name=txtPass size=4>"
    S = S & "<input type=button value=Connect onclick=Connect()>"
    S = S & "</form>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "</TABLE>"
    S = S & "</TD>"
    S = S & "<TD>"
    'TOOL DI GESTIONE WEBCAM
    S = S & "<TABLE id=TABLE1 cellSpacing=1 cellPadding=1 width=200 align=center border=0>"
    S = S & "<TR>"
    S = S & "<TD colspan=3>"
    S = S & "<p align=center><FONT face=Verdana size=1>Tool Webcam</FONT></P>"
    S = S & "<tr>"
    S = S & "<td width=56 height=28 align=middle>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzLU() type=button value=' Left up ' name=leftup>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<p align=center>"
    S = S & "<td width=61 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzU() type=button value=' UP ' name=up>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<td width=57 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzRU() type=button value=' Right up ' name=rightup>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "<tr>"
    S = S & "<td width=56 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzL() type=button value=' Left ' name=left>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<td width=61 height=28>"
    S = S & "<p align=center><FONT face=Verdana size=1></FONT>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<td width=57 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzR() type=button value=' Right ' name=right>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "<tr>"
    S = S & "<td width=56 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzLD() type=button value='Left Down' name=leftdown>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<td width=61 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzD() type=button value=' Down ' name=down>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "<td width=57 height=28>"
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzRD() type=button value='Right Down' name=rightdown>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "<tr>"
    S = S & "<td colSpan=3>
    "
    'ZOOM WEBCAM
    S = S & "<TABLE cellSpacing=1 cellPadding=1 width=100% border=0>"
    S = S & "<TR>"
    S = S & "<TD align=middle><FONT face=Verdana size=1>Zoom</FONT>"
    S = S & "<p align=center>"
    S = S & "<input onmouseup=zoom_stop() onmousedown=zoom_in() type=button value=' + ' name=zoomin>"
    S = S & "<input onmouseup=zoom_stop() onmousedown=zoom_out() type=button value=' - ' name=zoomout>"
    S = S & "</P>"
    S = S & "</TD>"
    S = S & "</TABLE>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "</TABLE>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "<TR>"
    S = S & "<TD colspan=2>"
    'CHAT
    S = S & "<TABLE cellSpacing=1 cellPadding=1 align=center border=0 id=TABLE1>"
    S = S & "<TR>"
    S = S & "<TD>"
    S = S & "<P align=center><img src=chat.gif></P>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "</TABLE>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "</TABLE>"
    Response.Write S
    %>
    --------------------------

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2003
    Messaggi
    709
    Mmm... non conosco ASP (o qualunque cosa sia quella che mi hai postato) ma andando ad intuito direi che potresti provare qualcosa del genere:
    codice:
    <script language="JavaScript"> 
    <!-- 
    function Timer() { 
    alert ("Il tempo a tua disposizione per muovere la webcam è terminato"); 
    } 
    
    function Connect() 
    { 
    CominsCamX.SetCamData(form1.txtCamName.value, form1.txtIP.value, form1.txtPort.value, 
    form1.txtID.value, form1.txtPass.value); 
    CominsCamX.ConnectVideo(); 
    setTimeout('Timer()',30000) 
    } 
    
    // --> 
    
    var theUser; 
    var thePwd;
    
    function _valori(){ 
    this.user="" 
    this.pwd="" 
    this.stato="" 
    } 
    
    var dati = new Array() 
    dati[0] = new _valori() 
    dati[0].user = "fra" 
    dati[0].pwd = "fra" 
    dati[0].stato = "libero" 
    dati[1] = new _valori() 
    dati[1].user = "rossano" 
    dati[1].pwd = "rossano" 
    dati[1].stato = "libero" 
    dati[2] = new _valori() 
    dati[2].user = "angela" 
    dati[2].pwd = "angela" 
    dati[2].stato = "libero" 
    dati[3] = new _valori() 
    dati[3].user = "max" 
    dati[3].pwd = "max" 
    dati[3].stato = "libero" 
    
    for (var i=0; i<dati.length; i++) { 
    if (dati[i].stato=="libero") { 
    dati[i].stato="occupato"; 
    theUser=dati[i].user; 
    thePwd=dati[i].pwd;
    window.alert("utente "+theUser+" password "+thePwd+": modificato stato da libero a occupato"); 
    break; 
    } 
    } 
    
    
    </script> 
    <% 
    Response.Write (theUser) 
    'CONNESSIONE ALLA WEBCAM 
    S = S & "<BODY onload=Connect()>" 
    S = S & "<p align=center>OPZIONI</font></p>" 
    S = S & "<TABLE cellSpacing=1 cellPadding=1 width=100% border=0>" 
    S = S & "<TR>" 
    S = S & "<TD>" 
    'OBJECT WEBCAM 
    S = S & "<TABLE id=TABLE1 style=WIDTH: 400px; HEIGHT: 400px height=250 cellSpacing=1 cellPadding=1 width=200 align=center border=0>" 
    S = S & "<TR>" 
    S = S & "<TD>" 
    S = S & "<OBJECT ID='CominsCamX' width=320 height=240 CLASSID='CLSID:A1D73A93-3CFA-4047-8256-9B704AEF0F74' codebase='CominsCamX.cab#Version=1,0,1,3'>" 
    S = S & "</OBJECT>" 
    S = S & "<form name=form1>" 
    S = S & "<input type=text value=WebCam View1 name=txtCamName size=8>" 
    S = S & "<input type=text value=10.0.0.239 name=txtIP size=10>" 
    S = S & "<input type=text value=700 name=txtPort size=3>" 
    S = S & "<input type=text value='" & theUser & "' name=txtID size=4>" 
    S = S & "<input type=text value='" & thePwd & "' name=txtPass size=4>"
    S = S & "<input type=button value=Connect onclick=Connect()>" 
    S = S & "</form>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "</TABLE>" 
    S = S & "</TD>" 
    S = S & "<TD>" 
    'TOOL DI GESTIONE WEBCAM 
    S = S & "<TABLE id=TABLE1 cellSpacing=1 cellPadding=1 width=200 align=center border=0>" 
    S = S & "<TR>" 
    S = S & "<TD colspan=3>" 
    S = S & "<p align=center><FONT face=Verdana size=1>Tool Webcam</FONT></P>" 
    S = S & "<tr>" 
    S = S & "<td width=56 height=28 align=middle>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzLU() type=button value=' Left up ' name=leftup>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<p align=center>" 
    S = S & "<td width=61 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzU() type=button value=' UP ' name=up>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<td width=57 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzRU() type=button value=' Right up ' name=rightup>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "<tr>" 
    S = S & "<td width=56 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzL() type=button value=' Left ' name=left>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<td width=61 height=28>" 
    S = S & "<p align=center><FONT face=Verdana size=1></FONT> " 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<td width=57 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzR() type=button value=' Right ' name=right>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "<tr>" 
    S = S & "<td width=56 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzLD() type=button value='Left Down' name=leftdown>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<td width=61 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzD() type=button value=' Down ' name=down>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "<td width=57 height=28>" 
    S = S & "<p align=center><input onmouseup=ptzstop() onmousedown=ptzRD() type=button value='Right Down' name=rightdown>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "<tr>" 
    S = S & "<td colSpan=3>
    " 
    'ZOOM WEBCAM 
    S = S & "<TABLE cellSpacing=1 cellPadding=1 width=100% border=0>" 
    S = S & "<TR>" 
    S = S & "<TD align=middle><FONT face=Verdana size=1>Zoom</FONT>" 
    S = S & "<p align=center>" 
    S = S & "<input onmouseup=zoom_stop() onmousedown=zoom_in() type=button value=' + ' name=zoomin> " 
    S = S & "<input onmouseup=zoom_stop() onmousedown=zoom_out() type=button value=' - ' name=zoomout>" 
    S = S & "</P>" 
    S = S & "</TD>" 
    S = S & "</TABLE>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "</TABLE>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "<TR>" 
    S = S & "<TD colspan=2>" 
    'CHAT 
    S = S & "<TABLE cellSpacing=1 cellPadding=1 align=center border=0 id=TABLE1>" 
    S = S & "<TR>" 
    S = S & "<TD>" 
    S = S & "<P align=center><img src=chat.gif></P>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "</TABLE>" 
    S = S & "</TD>" 
    S = S & "</TR>" 
    S = S & "</TABLE>" 
    Response.Write S 
    %>
    Nel tuo caso, la notazione document.forms['form1'].elements['...'] non credo che possa funzionare perchè il form non esiste ma viene creato dinamicamente (CREDO)

  9. #9

    Problema risolto

    Ti allego il codice e grazie di tutto
    -------------------------------------
    'OBJECT WEBCAM
    S = S & "<p align=center>OPZIONI</font></p>"
    S = S & "<TABLE id=TABLE1 style=WIDTH: 400px; HEIGHT: 400px height=250 cellSpacing=1 cellPadding=1 width=200 align=center border=0>"
    S = S & "<TR>"
    S = S & "<TD>"
    S = S & "<OBJECT ID='CominsCamX' width=320 height=240 CLASSID='CLSID:A1D73A93-3CFA-4047-8256-9B704AEF0F74' codebase='CominsCamX.cab#Version=1,0,1,3'>"
    S = S & "</OBJECT>"
    S = S & "<form id=form1 name=form1>"
    S = S & "<input type=text value=WebCam View1 name=txtCamName size=8>"
    S = S & "<input type=text value=10.0.0.239 name=txtIP size=10>"
    S = S & "<input type=text value=700 name=txtPort size=3>"
    S = S & "<input type=text value='' id=txtID name=txtID size=4>"
    S = S & "<input type=text value='' id=txtPass name=txtPass size=4>"
    S = S & "<input type=button value=Connect onclick=Connect()>"
    S = S & "</form>"
    S = S & "</TD>"
    S = S & "</TR>"
    S = S & "</TABLE>"
    S = S & "</BODY>"
    Response.Write S
    %>
    <script language="JavaScript">

    function Connect()
    {
    CominsCamX.SetCamData(form1.txtCamName.value, form1.txtIP.value, form1.txtPort.value,
    form1.txtID.value, form1.txtPass.value);
    CominsCamX.ConnectVideo();
    }

    function _valori(){
    this.user=""
    this.pwd=""
    this.stato=""
    }

    var dati = new Array()
    dati[0] = new _valori()
    dati[0].user = "fra"
    dati[0].pwd = "fra"
    dati[0].stato = "libero"
    dati[1] = new _valori()
    dati[1].user = "rossano"
    dati[1].pwd = "rossano"
    dati[1].stato = "libero"
    dati[2] = new _valori()
    dati[2].user = "angela"
    dati[2].pwd = "angela"
    dati[2].stato = "libero"
    dati[3] = new _valori()
    dati[3].user = "max"
    dati[3].pwd = "max"
    dati[3].stato = "libero"

    for (var i=0; i<dati.length; i++)
    {
    if (dati[i].stato=="libero")
    {
    dati[i].stato="occupato";
    alert("utente "+dati[i].user+" password "+dati[i].pwd+": modificato stato da libero a occupato");
    document.form1.txtID.value = dati[i].user;
    document.form1.txtPass.value = dati[i].pwd;
    break;
    }
    }

    </script>
    -----------------------------------------

  10. #10

    Re: Array

    Originariamente inviato da Francesca
    function _valori(){
    this.user=""
    this.pwd=""
    this.stato=""
    }

    var dati = new Array()
    dati[0] = new _valori()
    dati[0].user = "fra"
    dati[0].pwd = "fra"
    dati[0].stato = "libero"
    Mi permetto di storcere il naso di fronte a questa sintassi: non ha molto senso secondo me.
    Prima dichiari un costruttore _valori e poi setti le sue proprietà in modo pubblico.
    A questo punto tanto valeva scrivere:
    codice:
    var dati = new Array();
      dati[0] = new Object();
      dati[0].user = "fra";
      dati[0].pwd = "fra";
      dati[0].stato = "libero";
    ...e fregarsene del costruttore _valori. (ricordati i punti e virgola finali!)

    La versione che avrei scritto io, però è questa:
    codice:
    function User(user, pwd, libero) {
      this.user = user;
      this.pwd = pwd;
      this.libero = libero;
    }
    		
    var dati = new Array(
      new User("fra", "fra", true),
      new User("fre", "fre", false),
      new User("fri", "fri", false),
      new User("fro", "fro", true),
      new User("fru", "fru", true)
    );
    Come vedi, con una riga per utente mi sistemo l'elemento nell'Array, perché nel costruttore User setto i valori passati in parametro come proprietà dell'oggetto creato.
    Inoltre non è mai bellissimo usare stringhe per definire proprietà booleane, ecco perché io ho usato il true/false. Usando i booleani puoi scrivere delle if molto compatte e leggibili, tipo questa:
    codice:
    if (dati[0].libero)
      alert(dati[0].user + " è libero.");

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.