Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Polecenie które zwróci wszystkie elementy wskazanego id, document.getElementById('x') zwraca tylko jeden element.
twojastara
post
Post #1





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 25.11.2014

Ostrzeżenie: (10%)
X----


Chciałbym znać polecenie, które zwróci mi wszystkie elementy o wskazanym id.

polecenie document.getElementsByTagName('input') zwraca

  1. <input type="number" id="inputISBN" name="1111" value="2" >
  2. <input type="number" id="inputISBN" name="2222" value="2" >
  3. <input type="submit" value="Zatwierdź zmiany">



polecenie document.getElementById('inputISBN') zwraca tylko jedna linię, pierwszą.
  1. <input type="number" id="inputISBN" name="1111" value="2" >


Go to the top of the page
+Quote Post
Raito
post
Post #2





Grupa: Zarejestrowani
Postów: 156
Pomógł: 19
Dołączył: 27.04.2014

Ostrzeżenie: (0%)
-----


Id powinno być unikalne...
Go to the top of the page
+Quote Post
twojastara
post
Post #3





Grupa: Zarejestrowani
Postów: 124
Pomógł: 0
Dołączył: 25.11.2014

Ostrzeżenie: (10%)
X----


no tak, klasa.

a jak zrobić pętle w JavaScript, która wypełni mi tablicę, gdzie klucz=>wartość będzie elementy[i].getAttribute('name')=>elementy[i].getAttribute('value')

niezdarnie mniej więcej coś takiego:

  1. var elementy = document.getElementsByClassName('inputISBN')
  2. function (){
  3. var o = [];
  4. for (var i = 0; i < elementy.length; i++){
  5. o[elementy[i].getAttribute('name')] = o[elementy[i].getAttribute('value')]
  6. }
  7. }



coś co w php byłoby tablicą asocjacyjną

  1. for (i=0; i<3; i++){
  2.  
  3. $tablica[elementy[i].getAttribute('name')]=elementy[i].getAttribute('value');
  4. }


Ten post edytował twojastara 25.04.2015, 20:22:13
Go to the top of the page
+Quote Post
slash^
post
Post #4





Grupa: Zarejestrowani
Postów: 278
Pomógł: 36
Dołączył: 9.04.2003
Skąd: Płock

Ostrzeżenie: (0%)
-----


W czystym JS masz for..in https://developer.mozilla.org/en-US/docs/We...ements/for...in
Albo each w jQuery https://api.jquery.com/jquery.each/
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.08.2025 - 16:29