2009-10-15 13 views
6

Sto provando a creare una casella di selezione dinamica in JavaScript con un intervallo di anni che inizia con "un po 'di anno e termina con l'anno corrente. C'è qualcosa come la classe range di Ruby in JavaScript o devo fare un ciclo attraverso gli anni usando un ciclo for?Intervallo di anni in JavaScript per una casella di selezione

Ecco quello che ho trovato anche se penso che sia un po 'troppo in considerazione in Ruby, posso solo usare un intervallo.

this.years = function(startYear){ 
     startYear = (typeof(startYear) == 'undefined') ? 1980 : startYear 
     var currentYear = new Date().getFullYear(); 
     var years = [] 
     for(var i=startYear;i<=currentYear;i++){ 
      years.push(i); 
     } 
     return years; 
    } 

risposta

17

JavaScript ha un oggetto Range, ma si riferisce a una porzione arbitraria del DOM e non è supportato in IE 6/7.

Se lo desideri, puoi semplificare la tua funzione, ma in realtà è lo stesso.

this.years = function(startYear) { 
      var currentYear = new Date().getFullYear(), years = []; 
      startYear = startYear || 1980; 

      while (startYear <= currentYear) { 
        years.push(startYear++); 
      } 

      return years; 
    } 
+0

Perfetto. Non avevo capito che potevi usare il || operatore invece di scherzare con typeof(). – rwilliams

+0

Ya, ma solo se false/null è un valore non valido per tutto ciò su cui stai lavorando. Questo è un metodo molto comune per fornire una soluzione per i parametri predefiniti. –

1

Purtroppo, no, non c'è nessuna funzione "range" in Javascript che è paragonabile a Ruby, quindi dovrete avere a che fare con un ciclo. Sembra che quello che stai facendo dovrebbe funzionare, però.

0

è possibile fornire un metodo di gamma in javascript, ma si avrebbe bisogno di usare un sacco di pagare per la sua inclusione nel codice sorgente.

var A = Array.from (-5,5) >>> valore di ritorno: (matrice) -5, -4, -3, -2, -1,0,1,2,3,4 , 5

var B = Array.from (10,100,10) >> valore restituito: (Array) 10,20,30,40,50,60,70,80,90,100

var C = Array.from ('a', 'z') >> valore restituito: (Matrice) a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p, q, r, s, t, u, v, w, x, y, z

Array.from= function(what, n, step){ 
    var A= []; 
    if(arguments.length){ 
     if(n){ 
      return A.range(what, n, step) 
     } 
     L= what.length; 
     if(L){ 
      while(L){ 
       A[--L]= what[L]; 
      } 
      return A; 
     } 
     if(what.hasOwnProperty){ 
      for(var p in what){ 
       if(what.hasOwnProperty(p)){ 
        A[A.length]= what[p]; 
       } 
      } 
      return A; 
     } 
    } 
    return A; 
} 

Array.prototype.range= function(what, n, step){ 
    this[this.length]= what; 
    if(what.split && n.split){ 
     what= what.charCodeAt(0); 
     n= n.charCodeAt(0); 
     while(what<n){ 
      this[this.length]= String.fromCharCode(++what); 
     } 
    } 
    else if(isFinite(what)){ 
     step= step || 1; 
     while(what <n) this[this.length]= what+= step; 
    } 
    return this; 
} 
Problemi correlati