Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zależne rozwijane listy, funkcja IF
kolenda
post
Post #1





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 7.11.2017

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


Witam,

Jestem początkujący i chciałbym zrobić dwie zależne od siebie listy. Chodzi o to, że pierwsza listwa:

<select name="1">
<option name="a">a </option>
<option name="b">b </option>
<option name="c">c </option>
</select>

Jak utworzyć warunek, żeby przy wybraniu "a" wyskakiwała konkretnie przypisana druga lista np.
Jeśli wybierzesz "a" to:

<select name="aa">
<option> a1</option>
<option> a2</option>
</select>

Proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
trzczy
post
Post #2





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


https://stackoverflow.com/questions/2767393...d-to-each-other
Go to the top of the page
+Quote Post
kolenda
post
Post #3





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 7.11.2017

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


Dzięki za podpowiedź, ale nawet jak wkleiłem tamten kod ze strony to wyskakują błedy:


var options2 = sel2.querySelectorAll('option'); -> Uncaught TypeError: Cannot read property 'querySelectorAll' of null

sel2.innerHTML = ''; -> Uncaught TypeError: Cannot set property 'innerHTML' of null


Z czym to jest związane?
Go to the top of the page
+Quote Post
viking
post
Post #4





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


Co zawiera zmienna sel? Pewnie przez brak id jest null


--------------------
Go to the top of the page
+Quote Post
kolenda
post
Post #5





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 7.11.2017

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


sel2 jest to id select'a

<select id="sel2">
Go to the top of the page
+Quote Post
viking
post
Post #6





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


console.log(sel2) i pokaż wynik (w konsoli przeglądarki).


--------------------
Go to the top of the page
+Quote Post
kolenda
post
Post #7





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 7.11.2017

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


formularz.js:3 Uncaught TypeError: Cannot read property 'querySelectorAll' of null
at formularz.js:3
(anonymous) @ formularz.js:3

Kod jest taki:

var sel1 = document.querySelector('#sel1');
var sel2 = document.querySelector('#sel2');
var options2 = sel2.querySelectorAll('option');

function giveSelection(selValue) {
sel2.innerHTML = '';
for (var i = 0; i < options2.length; i++) {
if (options2[i].dataset.option === selValue) {
sel2.appendChild(options2[i]);
}
}
}

giveSelection(sel1.value);


Natomiast html taki:

<select id="sel1" onchange="giveSelection(this.value)">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select id="sel2">
<option data-option="a">apple</option>
<option data-option="a">airplane</option>
<option data-option="b">banana</option>
<option data-option="b">book</option>
</select>
Go to the top of the page
+Quote Post
viking
post
Post #8





Grupa: Zarejestrowani
Postów: 6 380
Pomógł: 1116
Dołączył: 30.08.2006

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


A gdzie wrzuciłeś ten formularz.js? Na początku w head czy na koniec body? Jeśli w head i nie masz listenera na DOMContentLoaded to nie będzie działać bo nie ma jeszcze elementu do pobrania.


--------------------
Go to the top of the page
+Quote Post
kolenda
post
Post #9





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 7.11.2017

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


Działa!!! Dzięki wielkie!!!
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 Aktualny czas: 22.08.2025 - 05:41