Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Przekazywanie zmiennych do pola html
Foxim
post 8.08.2013, 10:50:54
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 8.08.2013

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


Witam,

Od dłuższej chwili borykam się z problemem i proszę was o pomoc.

Mam taki kod:
  1.  
  2.  
  3. <script language="javascript">
  4. function check (skill){
  5. if(parseInt (document.getElementById("SP").value) > 0)
  6. add(skill);
  7. else
  8. alert("U don't have skills points!");
  9. }
  10. function add (skill){
  11. if(skill < 101)
  12. alert("dziala");
  13. else if(skill > 100 && skill < 126)
  14. alert("dziala");
  15. else if(skill >125 && skill < 151)
  16. alert("dziala");
  17. else if(skill > 150 && skill < 176 )
  18. alert("dziala");
  19. else if(skill >175 && skill < 201)
  20. alert("dziala");
  21. else if(skill > 200 && skill < 301)
  22. alert("dziala");
  23. else
  24. return;
  25. }
  26.  
  27. <input type='text' class="stats" id='SP' value='5' readonly="yes" > </h1>
  28. Klik <input type='text' class='skills' id="OUT" value='20' readonly="yes" > %
  29. <input type='button' class="skills" name='add' onclick='check(OUT.value)' value='+'/>
  30. <input type='button' class="skills" name='subtract' onclick='check(OUT.value)' value='-'/></p></h3>
  31.  
  32. </body>
  33. </html>


I wszystko działa problem polega pojawia się gdy chcę uzyskać coś takiego:
  1.  
  2. }
  3. function add (skill){
  4. if(skill < 101)
  5. skill = skill +1;
  6. return skill;
  7. else if(skill > 100 && skill < 126)
  8. alert("dziala");
  9. else if(skill >125 && skill < 151)
  10. alert("dziala");
  11. else if(skill > 150 && skill < 176 )
  12. alert("dziala");
  13. else if(skill >175 && skill < 201)
  14. alert("dziala");
  15. else if(skill > 200 && skill < 301)
  16. alert("dziala");
  17. else
  18. return;
  19. }



Chodzi o to aby funkcja zwracała wartość zmiennej do pola(w tym przypadku OUT) z której uzyskała wartość.
Nie mogę użyć getElementById("") (w taki sposób jaki ja potrafie jej użyć) gdyż dla każdego przypadku musiałbym tworzyć osobną funkcje.

Pozdrawiam.
Go to the top of the page
+Quote Post
kaznodziej/ka
post 8.08.2013, 13:38:31
Post #2





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 28.03.2013

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


var wynik = skil;
var txt_out = document.getElementById('OUT');
txt_out.value = wynik;

lub zwyczajnie:
var txt_out = document.getElementById('OUT');
txt_out.value = skil;

Pozdrawiam
Go to the top of the page
+Quote Post
Foxim
post 8.08.2013, 16:02:25
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 8.08.2013

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


Cytat(kaznodziej/ka @ 8.08.2013, 14:38:31 ) *
var wynik = skil;
var txt_out = document.getElementById('OUT');
txt_out.value = wynik;

lub zwyczajnie:
var txt_out = document.getElementById('OUT');
txt_out.value = skil;

Pozdrawiam



Może inaczej zadam pytanie.

  1.  
  2. <script language="javascript">
  3. function check (skill){
  4. if(parseInt (document.getElementById("SP").value) > 0)
  5. add(skill);
  6. else
  7. alert("U don't have skills points!");
  8. }
  9. function add (skill){
  10. var wynik = skill +1;
  11. var txt_out = document.getElementById('OUT');
  12. if(skill < 101)
  13. txt_out.value = wynik;
  14. //else if(skill > 100 && skill < 126)
  15. // alert("dziala");
  16. //else if(skill > 125 && skill < 151)
  17. // alert("dziala");
  18. //else if(skill > 150 && skill < 176 )
  19. // alert("dziala");
  20. //else if(skill >175 && skill < 201)
  21. // alert("dziala");
  22. //else if(skill > 200 && skill < 301)
  23. // alert("dziala");
  24. else
  25. return;
  26. }
  27.  


Jak najbardziej działa jednak w momencie kiedy dodam cos takiego:

  1.  
  2. <input type='text' class="stats" id='SP' value='5' readonly="yes" > </h1>
  3. Klik <input type='text' class='skills' id="OUT" value='20' readonly="yes" > %
  4. <input type='button' class="skills" name='add' onclick='check(OUT.value)' value='+'/>
  5. <input type='button' class="skills" name='subtract' onclick='check(OUT.value)' value='-'/></p></h3>
  6.  
  7. Klik2 <input type='text' class='skills' id="LD" value='20' readonly="yes" > %
  8. <input type='button' class="skills" name='add' onclick='check(LD.value)' value='+'/>
  9. <input type='button' class="skills" name='subtract' onclick='check(LD.value)' value='-'/></p></h3>
  10.  


Funkcja zadziała tylko dla pierwszego przypadku.
Go to the top of the page
+Quote Post
kaznodziej/ka
post 13.08.2013, 10:09:56
Post #4





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 28.03.2013

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


dużo ma być tych "przypadków" questionmark.gif... bo możnaby dodać odwołanie do każdego ID w funkcji. (?)

Pozdrawiam.

Ten post edytował kaznodziej/ka 13.08.2013, 10:25:57
Go to the top of the page
+Quote Post
Foxim
post 17.08.2013, 12:47:58
Post #5





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 8.08.2013

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


Cytat(kaznodziej/ka @ 13.08.2013, 11:09:56 ) *
dużo ma być tych "przypadków" questionmark.gif... bo możnaby dodać odwołanie do każdego ID w funkcji. (?)

Pozdrawiam.



Rozwiązałem problem "na piechote" (1 funkcja dla każdego pola) jednak gdyby ktoś miał pomysł jak to zrobić w inny sposób chętnie posłucham.
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: 14.08.2025 - 13:12