Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [js]Tworzenie nowej listy z listy
skowron-line
post
Post #1





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

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


Witam chcialbym utworzyc nowy obiekt typu <select> w oparciu o juz istniejacy, niby dosyc proste ale jednak.
kod
  1. <head>
  2. <script type="text/javascript">
  3. function showOptions(){
  4. var oList = document.getElementById('abc');
  5. var oDiv = document.getElementById('oDiv');
  6. var select = document.createElement('select');
  7. for(var i=0; i<=oList.length; i++){
  8. // alert(oList.options[i].value);
  9. // oListValue = oList.options[i].value;
  10. //oListText = oList.options[i].text;
  11. option = document.createElement('option');
  12. option.setAttribute('value',oList.options[i].value);
  13. var optionText = document.createTextNode(oList.options[i].text);
  14. option.appendChild(optionText);
  15. select.appendChild(option);
  16. }
  17. oDiv.appendChild(select);
  18. }
  19. </script>
  20. </head>
  21. <body onLoad="showOptions();">
  22. <select id="abc">
  23. <option value="a">ab</option>
  24. <option value="b">bc</option>
  25. <option value="c">cd</option>
  26. </select>
  27. <div id="oDiv"></div>
  28. </body>
  29. </html>

problem wyglada nastepujaco.
Kod
Error: oList.options[i] has no properties
Source File: file:///C:/AppServ/www/javascript/selectOptions.html
Line: 13

ale jak odkomentuje alerta to elegancko wywali on wszystkie wartosci tej listy
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Kod
var oOption2 = document.createElement('option');
        oOption2.setAttribute('value',oList2Value[i]);
        oOption2 = document.createTextNode(oList2Text[i]);
        oSelect2.appendChild(oOption2);

I przyjrzyj sie linijce 3 i temu co tam robisz

ps:
Cytat
ten sam sens ale inaczej napisane wiec nie rozumiem dlaczego nie dziala
A to ciekawe stwierdzenie (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Od kiedy ten sam sens idzie w parze z tą samą poprawnością kodu?
Go to the top of the page
+Quote Post

Posty w temacie


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: 6.10.2025 - 09:55