Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JS] Zablokowanie kilku inputów
Forum PHP.pl > Forum > Po stronie przeglądarki
Hectic
Mam 5 inputów o id='g5'. Czy da się je wszystkie zablokować naraz? Zrobiłem coś takiego

Kod
function acc(group)
{  
    val = document.getElementById(group).disabled;
    
   if(val == true) {
       document.getElementById(group).disabled=false;
   }
   else {
       document.getElementById(group).disabled=true;
   }
}


Ale po wywołaniu funkcji
Cytat
acc("g5")
blokuje się tylko pierwszy input sad.gif
shovit
Z tego co wiem id jest identyfikatorem danego elementu. Takze w dokumencie kazdy element musi miec inne id. Dlatego funkcja getElementById pobiera pierwszy który znajdzie.
Hectic
Bardziej chodziło mi o to czy ktoś mam pomysł na rozwiązanie tego problemu w inny sposób smile.gif
flv
Kod
function acc(it)
{  
    val = it.disabled;
    
   if(val == true) {
it.disabled=false;
   }
   else {
    it.disabled=true;
   }
}

i przekazuj przez funkcje referencje do 'this' (mozna wywalic id), jeśli nie możesz operować na 'this' bo odwolujesz sie do innego węzła to możesz zrobić klase i odwolywac sie przez getElementsByClassName zamiast getElementById
Hectic
Idąc za Twoją radą zrobiłem coś takiego

Kod
function getElementsByClassName(cn){
  var arr = new Array();
  var els = document.getElementsByTagName("*");
  var exp= new RegExp("^(.* )?"+cn+"( .*)?$", "g");
  for (var i = 0; i < els.length; i++ ){
    if (exp.test(els[i].className)){
      arr.push(els[i]);
    }
  }
  return arr;
}


Kod
function acc(group)
{  
    val = getElementsByClassName(group);
    
    all = val.length;
    
    for($a = 0; $a < all; $a++)
    {
        inpu = val[$a].name;
        disable = document.getElementById(inpu).disabled;
    
        if(disable == true) {
               document.getElementById(inpu).disabled=false;
           }
           else {
               document.getElementById(inpu).disabled=true;
           }
    }
  
}

Nawet działa smile.gif Może dało się to zrobić dużo prościej, ale w JS jestem początkujący winksmiley.jpg
Dzięki Rkingsmiley.png
abc667
albo
  1. function acc(string)
  2. {
  3. var inputs = string.split(",");
  4.  
  5. for(var i=0;i<inputs.length;i++)
  6. {
  7. document.getElementById(inputs[i]).disabled = (document.getElementById(inputs[i]).disabled ? '' : 'disabled');
  8. }
  9. }


i teraz przekazujesz jako parametr listę id, kolejne id oddzielone przecinkami
np
  1. <a href="#" onclick="acc('g1,g2,g3,g4,g5'); return false;">b</a>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.