Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript] Pobieranie wartości atrybutu
MeGusta
post 12.12.2015, 14:43:32
Post #1





Grupa: Zarejestrowani
Postów: 155
Pomógł: 0
Dołączył: 6.07.2015

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


Witam, jak mogę pobrać wartość klikniętego atrybutu??

Próbowałem tak:
  1. echo '<option id="path" path="'.$i['path'].'" id="'.$i['id'].'" value="'.$i['id'].'">'.$i['name'].'</option>';
  2.  
  3. $('#option_items').click(function(){
  4.  
  5. $this = $(this);
  6.  
  7. var a = document.getElementById($this).getAttribute('path');
  8.  
  9. $('#img_itm').html('<img src="'+ a +'"/>');
  10.  
  11.  
  12. });


Lecz wartość null.
Go to the top of the page
+Quote Post
casperii
post 12.12.2015, 14:49:31
Post #2





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


zobacz:

  1. var $zmienna = $("#path").val();


tylko że ID powinno być nadane na select
Go to the top of the page
+Quote Post
MeGusta
post 12.12.2015, 14:51:40
Post #3





Grupa: Zarejestrowani
Postów: 155
Pomógł: 0
Dołączył: 6.07.2015

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


Zauważ że chciałbym pobrać wartość atrybutu path z <option path="" .... >
Go to the top of the page
+Quote Post
koodo218
post 12.12.2015, 14:58:43
Post #4





Grupa: Zarejestrowani
Postów: 114
Pomógł: 25
Dołączył: 22.11.2015

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


  1. var wartosc = document.getElementById("path").getAttribute("path");


--------------------
Słaba znajomość (ale się staram): HTML5, CSS3, JavaScript, SQL, Access
Go to the top of the page
+Quote Post
MeGusta
post 12.12.2015, 15:09:19
Post #5





Grupa: Zarejestrowani
Postów: 155
Pomógł: 0
Dołączył: 6.07.2015

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


Okej dzięki, tylko teraz pobiera mi wartość path tylko pierwsze wartości, a ja chciałbym aby pobierało według klikniętego option.
Go to the top of the page
+Quote Post
koodo218
post 12.12.2015, 15:26:54
Post #6





Grupa: Zarejestrowani
Postów: 114
Pomógł: 25
Dołączył: 22.11.2015

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


  1. <select id="nazwa_selecta">
  2. <option path="1">1</option>
  3. <option path="2">2</option>
  4. <option path="3">3</option>
  5. <option path="4">4</option>
  6. <option path="5">5</option>
  7. <div id="obrazek"></div>
  8. document.getElementById("nazwa_selecta").onchange = obsluga_option;
  9. function obsluga_option(){
  10. var aktualny = document.getElementById("nazwa_selecta").options[document.getElementById("nazwa_selecta").selectedIndex].getAttribute("path");
  11. document.getElementById("obrazek").innerHTML = "<img src=\""+aktualny+"\"/>";
  12. }


--------------------
Słaba znajomość (ale się staram): HTML5, CSS3, JavaScript, SQL, Access
Go to the top of the page
+Quote Post
droslaw
post 12.12.2015, 16:03:06
Post #7





Grupa: Zarejestrowani
Postów: 98
Pomógł: 33
Dołączył: 10.05.2011
Skąd: Krak

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


koodo218 już podał działające rozwiązanie, ale ja mam to w jQuery. Poza tym obrazek ładuje się od razu po załadowaniu strony, nie trzeba czekać na wybranie elementu z listy.

http://jsfiddle.net/droslaw/7bd9LkLa/

  1. <select id="images">
  2. <option data-path="http://www.ggowo.fora.pl/images/galleries/10410633794a17d752a495a-076434-wm.png">smile</option>
  3. <option selected data-path="http://2.bp.blogspot.com/-IqXVUpDecOg/TWJKlVZQ3ZI/AAAAAAAAAwk/JgQ4rZvA2iw/s1600/Angry-Birds.png">angry</option>
  4. </select>
  5. </form>
  6. var $select, $img_container;
  7.  
  8. $select = $( "#images" );
  9. $img_container = $('#img_itm');
  10.  
  11. $select.change(function() {
  12. show_selected_image(this);
  13. });
  14.  
  15. $(document).ready(function () {
  16. show_selected_image($select);
  17. });
  18.  
  19. function show_selected_image (list) {
  20. var img_src;
  21. img_src = $(list).find(':selected').attr('data-path');
  22. $img_container.html('<img src="' + img_src + '"/>');
  23. }


Ten post edytował droslaw 12.12.2015, 16:06:01
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 10.06.2025 - 05:15